0

I have for example this JSON string:

{"name":"Nataraj", "job":"Programmer","property":["chair":"my","table":"brothers","cabinet":"mothers"]}

and, i want to get this data to class as:

public class A{
  String name;
  String job;
  //Map<String, String> property; (Maybe: <String, Object>?)
}

How do I get the data to this map? The main problem I have with "property". I used JSONObject and ObjectMapper, but on the property i have problem. Of course, every variable in class A has getter and setter. Any help for me?

6
  • Use JSONObject from org.json:json library or use jackson or gson. Commented Aug 20, 2019 at 16:22
  • 2
    It's not a valid JSON string, there's no such thing as associative arrays in JSON. Are you sure it's not {..., "property": { "chair":"my", ...}} ? Commented Aug 20, 2019 at 16:22
  • It's an invalid json Commented Aug 20, 2019 at 16:27
  • I use json and gson, but in any tutorial and example is not a something as here, from property is one element if i use it...after that id this: jsonOb.getString("name") with incorrect exit Commented Aug 20, 2019 at 16:28
  • yes it is with error, i forget to {} and something...but the problem is same, i do not remeber right shape Commented Aug 20, 2019 at 16:30

2 Answers 2

1

First of all your JSON has to be modified a bit. The property is not an Array rather an Object containing key-value pairs. It should be like this:

{"name":"Nataraj", "job":"Programmer","property":
{"chair":"my","table":"brothers","cabinet":"mothers"}}

As I mentioned in the comment to original post, you may use org.json:json library or Jackson or Gson library to parse any JSON string and populate your objects.

Here is the executable program that parses your JSON using basic org.json:json library and populates the data structure you expect:

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class ParseJsonIntoMap {
    public static void main(String[] args) {
        String data="{\"name\":\"Nataraj\", \"job\":\"Programmer\",\"property\":{\"chair\":\"my\",\"table\":\"brothers\",\"cabinet\":\"mothers\"}}";
        JSONObject parsedData = new JSONObject(data);
        A a = new A();
        a.setName(parsedData.getString("name"));
        a.setJob(parsedData.getString("job"));
        JSONObject propertyMap = parsedData.getJSONObject("property");
        for (String key : propertyMap.keySet()) {
            a.getProperty().put(key, propertyMap.getString(key));
        }
        System.out.println("Output: "+a);
    }

    public static class A{
        String name;
        String job;
        Map<String, String> property = new HashMap<>();

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getJob() {
            return job;
        }

        public void setJob(String job) {
            this.job = job;
        }

        public Map<String, String> getProperty() {
            return property;
        }

        public void setProperty(Map<String, String> property) {
            this.property = property;
        }

        @Override
        public String toString() {
            return "A{" +
                    "name='" + name + '\'' +
                    ", job='" + job + '\'' +
                    ", property=" + property +
                    '}';
        }
    }
}

You should add Maven (or equivalent Gradle or other) dependency to:

<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20180813</version>
</dependency>
Sign up to request clarification or add additional context in comments.

Comments

0

The following works just fine with Gson 2.8.5 :

A instance = new Gson().fromJson(data, A.class)

The full class for you to try :

import java.util.Map;
import com.google.gson.Gson;

public class Main {

    public static void main(String[] args) {
        String data="{\"name\":\"Nataraj\", \"job\":\"Programmer\",\"property\":{\"chair\":\"my\",\"table\":\"brothers\",\"cabinet\":\"mothers\"}}";
        System.out.println(new Gson().fromJson(data, A.class));
    }

    static class A{
        String name;
        String job;
        Map<String, String> property;

        @Override
        public String toString() { // just there for the System.out.println in main
            return String.format("name: %s, job: %s, property: %s", name, job, property);
        }
    }
}

Most other JSON parsing libraries provide similar functionality.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.