2016-07-21 48 views
0

複雜的JSON我有這個JSON響應如何分析利用GSON和Java

{ 
"data":{ 
    "last_name":"abcdef", 
    "email":"[email protected]", 
    "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字典?

+1

參考這個http://www.mkyong.com/java/how -do-convert-java-object-to-from-json-format-gson-api/ – vinoth12594

回答

-1

試試這個:

Gson gson = new Gson(); 
ABC abc = gson.fromJson(responseObject, ABC.class); 

ABC是你想要的對象。

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); 
+0

我該如何訪問JAVA? details.JAVA ??? –

+0

我認爲這應該是答案。 @kashyapkbandi,你可以使用'details.data.data1.Skills.JAVA'來獲得'JAVA'的值。 –

+0

details.data.data1.Skills.get(0).JAVA //技能是列表對象,因此您可以通過索引來檢索Skill對象 –

-1

試試這個:

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(); 
    } 

ABC.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; 
} 
} 

Skills.java

public class Skills { 
private String c; 
private String cPlusPlus; 
private String java; 
private String web; 

public String getC() { 
    return c; 
} 

public void setC(String c) { 
    this.c = c; 
} 

public String getcPlusPlus() { 
    return cPlusPlus; 
} 

public void setcPlusPlus(String cPlusPlus) { 
    this.cPlusPlus = cPlusPlus; 
} 

public String getJava() { 
    return java; 
} 

public void setJava(String java) { 
    this.java = java; 
} 

public String getWeb() { 
    return web; 
} 

public void setWeb(String web) { 
    this.web = web; 
} 
}