下面是我的JSON字符串,我正在尝试使用GSON解析此JSON字符串,因为我发现它很容易使用。
{
"HasChanged":true,
"Version":1,
"LastModifiedDate":1390561121310,
"DATACENTERS":
[
{
"Name": "DC1",
"TotalNumberOfServers":4,
"PrimaryData":[{"0":"1", "1":"2", "2":"3", "3":"4"}],
"SecondaryData":[{"0":"2", "1":"3", "2":"4", "3":"1"}],
"MachineMapping":[{"3":"dc11115.dc1.domain.com"}, {"2":"dc11114.dc1.domain.com"}, {"1":"dc11113.dc1.domain.com"}, {"4":"dc11116.dc1.domain.com"}]
},
{
"Name": "DC2",
"TotalNumberOfServers":4,
"PrimaryData":[{"1":"1", "5":"2", "2":"3", "6":"4"}],
"SecondaryData":[{"7":"1", "0":"8", "2":"5", "10":"9"}],
"MachineMapping":[{"3":"dc21147.dc2.domain.com"}, {"2":"dc21146.dc2.domain.com"}, {"1":"dc21145.dc2.domain.com"}, {"4":"dc21148.dc2.domain.com"}]
}
]
}
解析上面的JSON字符串后,我需要将每个DataCenter
数据存储在以下数据结构中 -
Map<String, Map<String, String>> primaryData
Map<String, Map<String, String>> secondaryData
Map<String, Map<String, String>> machineMapping
对于JSON字符串中的PrimaryData
标记 -
Here Key of `primaryData` map is `DC1` and value is {"0":"1", "1":"2", "2":"3", "3":"4"} which is for `PrimaryData` tag.
Similarly, another Key of `primaryData` map is `DC2` and value is {"1":"1", "5":"2", "2":"3", "6":"4"} which is for `PrimaryData` tag.
还有JSON字符串中的SecondaryData
标记 -
Here Key of `secondaryData` map is `DC1` and value is {"0":"2", "1":"3", "2":"4", "3":"1"} which is for `SecondaryData` tag.
Similarly, another Key of `secondaryData` map is `DC2` and value is {"7":"1", "0":"8", "2":"5", "10":"9"} which is for `SecondaryData` tag.
还有JSON字符串中的MachineMapping
标记 -
Here Key of `machineMapping` is `DC1` and value is {"3":"dc11115.dc1.domain.com"}, {"2":"dc11114.dc1.domain.com"}, {"1":"dc11113.dc1.domain.com"}, {"4":"dc11116.dc1.domain.com"} which is for MachineMapping tag.
Similarly another key of `machineMapping` is `DC2` and value is {"3":"dc21147.dc2.domain.com"}, {"2":"dc21146.dc2.domain.com"}, {"1":"dc21145.dc2.domain.com"}, {"4":"dc21148.dc2.domain.com"} which is for `MachineMapping` tag.
这是我第一次使用这个复杂的JSON字符串进行解析,因此我开始使用GSON,因为我发现它很容易使用。这是我到目前为止尝试过的,我能够提取大部分字段,但不确定如何将PrimaryData
对象映射到Map
,然后为其他人做同样的事情?
private static Map<String, Map<String, String>> primaryData = new LinkedHashMap<String, Map<String, String>>();
private static Map<String, Map<String, String>> secondaryData = new LinkedHashMap<String, Map<String, String>>();
private static Map<String, Map<String, String>> machineMappedData = new LinkedHashMap<String, Map<String, String>>();
public static void main(String[] args) {
String jsonLine = "my json string";
JsonElement jelement = new JsonParser().parse(jsonLine);
JsonObject jobject = jelement.getAsJsonObject();
boolean changed = jobject.get("HasChanged").getAsBoolean();
String timestamp = jobject.get("LastModifiedDate").getAsString();
String version = jobject.get("Version").getAsString();
System.out.println(changed);
System.out.println(timestamp);
System.out.println(version);
if (changed) {
JsonArray jarray = jobject.getAsJsonArray("DATACENTERS");
for (int i = 0; i < jarray.size(); i++) {
Map<String, String> data = new LinkedHashMap<String, String>();
jobject = jarray.get(i).getAsJsonObject();
String dcName = jobject.get("Name").getAsString();
int servers = jobject.get("TotalNumberOfServers").getAsInt();
System.out.println(dcName);
System.out.println(servers);
if (servers > 0) {
JsonArray jarray1 = jobject.getAsJsonArray("PrimaryData");
System.out.println(jarray1);
for (int j = 0; j < jarray1.size(); j++) {
jobject = jarray1.get(j).getAsJsonObject();
System.out.println(jobject);
// not sure how to map the PrimaryData object to a Map here?
}
}
}
}
}
但我一直坚持下去并且不确定如何将JSON对象放在Map中?任何人都可以帮我这个吗?
答案 0 :(得分:1)
您应该做的是定义JSON映射到的Java类:
public class MyJsonResponse
{
private boolean HasChanged;
private int Version;
private long LastModifiedDate;
private List<DataCenter> DATACENTERS;
// Getters/Setters for above
}
public class DataCenter
{
private String Name;
private int TotalNumberOfServers;
private List<Map<String,String>> PrimaryData;
private List<Map<String,String>> SecondaryData;
private List<Map<String,String>> MachineMapping;
// getters and setters for all of the above
}
定义了这些类后,它就像:
一样简单MyJsonResponse response = new Gson().fromJson(jsonString, MyJsonResponse.class);
另外值得注意的是,由于JSON变量名称不符合Java命名约定,因此 也应通过@SerializedName
注释转换它们。
@SerializedName("DATACENTERS")
private List<DataCenter> dataCenters;
作为一个例子。