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?
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>
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.