我有JSON
回复
{
"data":{
"last_name":"abcdef",
"email":"xyz@zzz.com",
"data1":{
"data2":{
"membership":"senior"
},
"Skills":[
{
"C":"GOOD",
"C++":"AVERAGE",
"JAVA":"GOOD",
"WEB":"GOOD"
}
]
}
}
}
现在我使用以下方法通过GSON
public class ABC {
public String last_name;
public String email;
.
.
.
}
然后我做
让我们考虑JSON OBJECT是== responseObject
ABC obj = (ABC)responseObject // TypeCasting
obj.last_name
给出了姓氏的结果
如何实现数组和其他data1字典?
答案 0 :(得分:1)
您可以使用gson
库,这样可以更轻松地解析,也很快。
制作下面的类,它会将json转换为你的java对象
public class Details {
public MyData data;
}
public class MyData{
public String last_name;
public String email;
public MyData1 data1;
}
public class MyData1 {
public MyData2 data2;
public List<Skill> Skills;
}
public class Mydata2{
public string membership;
}
public class Skill {
public String C;
public String C++;
public String JAVA;
public String WEB;
}
// call following statement where you want to parse
Details details= new Gson().fromJson(json, Details.class);
答案 1 :(得分:-1)
试试这个:
im = ax.imshow(data)
im.set_extent((0, new_data.shape[1], new_data.shape[0], 0))
im.set_data(new_data)
ABC.java
try {
JSONObject object = new JSONObject(jsonString);
if (object.has("data")) {
JSONObject dataObject = object.getJSONObject("data");
String name = "", email = "", membership = "";
if (dataObject.has("last_name")) {
name = dataObject.getString("last_name");
}
if (dataObject.has("email")) {
email = dataObject.getString("email");
}
ABC abc = new ABC();
abc.setLast_name(name);
abc.setEmail(email);
abc.setMembership(membership);
if (dataObject.has("data1")) {
JSONObject data1JsonObject = dataObject.getJSONObject("data1");
if (data1JsonObject.has("data2")) {
membership = data1JsonObject.getJSONObject("data2").getString("membership");
}
if (data1JsonObject.has("Skills")) {
JSONArray jsonArray = data1JsonObject.getJSONArray("Skills");
Skills skills = new Skills();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject skillsJsonObject = jsonArray.getJSONObject(i);
if (skillsJsonObject.has("C")) {
skills.setC(skillsJsonObject.getString("C"));
}
if (skillsJsonObject.has("C++")) {
skills.setcPlusPlus(skillsJsonObject.getString("C++"));
}
if (skillsJsonObject.has("JAVA")) {
skills.setJava(skillsJsonObject.getString("JAVA"));
}
if (skillsJsonObject.has("WEB")) {
skills.setWeb(skillsJsonObject.getString("WEB"));
}
}
abc.setSkills(skills);
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
Skills.java
public class ABC {
private String last_name;
private String email;
private String membership;
private Skills skills;
public String getLast_name() {
return last_name;
}
public void setLast_name(String last_name) {
this.last_name = last_name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getMembership() {
return membership;
}
public void setMembership(String membership) {
this.membership = membership;
}
public Skills getSkills() {
return skills;
}
public void setSkills(Skills skills) {
this.skills = skills;
}
}