2016-09-20 99 views
0

我能夠解析我需要的所有東西,除了target_id的field_exercis_arc
我得到了nid,標題和正文。不知道如何獲取field_exercis_arc中的id。在Android中解析JSON,未獲取值


的JSON

[{ 
"nid": "26", 
"title": "Question test", 
"body": "xcvxcv", 
"field_exercis_arc": ["25","27"] 
}] 

守則

String finalJson = buffer.toString(); 
      JSONArray parentArray = new JSONArray(finalJson); 
      List<ExerciseModel> exerciseModelList = new ArrayList<>(); 

      for(int i=0; i<parentArray.length(); i++){ 
       JSONObject finalObject = parentArray.getJSONObject(i); 

       title_exi = finalObject.getString("title"); 
       text_exi = finalObject.getString("body"); 
       //This part is working. 
       ExerciseModel exerciseModel = new ExerciseModel(); 
       exerciseModel.setTitle(finalObject.getString("title")); 
       exerciseModel.setNid(finalObject.getInt("nid")); 
       exerciseModel.setBody(finalObject.getString("body")); 

       //Problem with this part, not getting the target_id's. 
       List<ExerciseModel.Exer> exerList = new ArrayList<>(); 
       for(int j=0; j<finalObject.getJSONArray("field_exercis_arc").length(); j++){ 
        ExerciseModel.Exer exercis = new ExerciseModel.Exer(); 
        exercis.setTarget_id(finalObject.getJSONArray("field_exercis_arc").getJSONObject(j).getString("target_id")); 
        exerList.add(exercis); 
       } 
       exerciseModel.setExerList(exerList); 
       exerciseModelList.add(exerciseModel); 

       mDB.saveRecordEX(exerciseModel); 
      } 

的field_exercis_arc和target_id的領域模型

private List<Exer> exerList; 
public List<Exer> getExerList() { 
    return exerList; 
} 

public void setExerList(List<Exer> exerList) { 
    this.exerList = exerList; 
} 

public static class Exer{ 
    private String target_id; 
    public String getTarget_id() { 
     return target_id; 
    } 
    public void setTarget_id(String target_id) { 
     this.target_id = target_id; 
    } 
} 

在此先感謝

+0

您的JSON是無法驗證PLZ檢查 –

回答

0

使用下面的代碼:

exercis.setTarget_id(finalObject.getJSONArray("field_exercis_arc").getString(j)); 
1

我建議您使用GSON庫從JSON獲取結果。爲此,您需要Java類才能將結果解析爲對象。爲此,您可以使用JSON轉換爲Java類here

爲您示例類是:

public class Und 
{ 
    private String value; 

    public String getValue() { return this.value; } 

    public void setValue(String value) { this.value = value; } 
} 

public class Body 
{ 
    private ArrayList<Und> und; 

    public ArrayList<Und> getUnd() { return this.und; } 

    public void setUnd(ArrayList<Und> und) { this.und = und; } 
} 

public class Und2 
{ 
    private String target_id; 

    public String getTargetId() { return this.target_id; } 

    public void setTargetId(String target_id) { this.target_id = target_id; } 
} 

public class FieldExercisArc 
{ 
    private ArrayList<Und2> und; 

    public ArrayList<Und2> getUnd() { return this.und; } 

    public void setUnd(ArrayList<Und2> und) { this.und = und; } 
} 

public class RootObject 
{ 
    private String vid; 

    public String getVid() { return this.vid; } 

    public void setVid(String vid) { this.vid = vid; } 

    private String uid; 

    public String getUid() { return this.uid; } 

    public void setUid(String uid) { this.uid = uid; } 

    private String title; 

    public String getTitle() { return this.title; } 

    public void setTitle(String title) { this.title = title; } 

    private Body body; 

    public Body getBody() { return this.body; } 

    public void setBody(Body body) { this.body = body; } 

    private FieldExercisArc field_exercis_arc; 

    public FieldExercisArc getFieldExercisArc() { return this.field_exercis_arc; } 

    public void setFieldExercisArc(FieldExercisArc field_exercis_arc) { this.field_exercis_arc = field_exercis_arc; } 

    private String cid; 

    public String getCid() { return this.cid; } 

    public void setCid(String cid) { this.cid = cid; } 

    private String last_comment_timestamp; 

    public String getLastCommentTimestamp() { return this.last_comment_timestamp; } 

    public void setLastCommentTimestamp(String last_comment_timestamp) { this.last_comment_timestamp = last_comment_timestamp; } 
} 

可以結果轉換爲RootObject。福克斯例如:

String json = "{\"vid\": \"26\",\"uid\": \"1\",\"title\": \"Question test\",\"body\": {\"und\": [{\"value\": \"xcvxcv\"}]},\"field_exercis_arc\": {\"und\": [{\"target_id\": \"25\"},{\"target_id\":\"27\"}]},\"cid\": \"0\",\"last_comment_timestamp\": \"1472217577\"}"; 
RootObject object = new Gson().fromJson(json, RootObject.class); 
System.out.println("Title is: "+object.getTitle()); 

結果是:

Title is: Question test 

這之後,您可以使用您的對象來獲取從JSON任何價值。

另外你應該知道你的JSON是無效的。你在兩個不應該存在的地方有逗號。在字符串我給你上面那些是固定的。你應該檢查你的JSON:JSON Formatter

+0

其實我嘗試過實施GSON,但我得到了一些錯誤。這是我關於GSON的文章。 http://stackoverflow.com/questions/39161354/gson-array-error-message-expected-a-string-but-was-begin-array 我會再試一次。也許我這次很幸運 –

+0

在這個鏈接上,我看到你也使用JSONArray,然後你迭代槽數組,然後使用Gson。不是很好。您應該立即使用Gson將json轉換爲對象,就像我在之前的回答中給出了一個示例。你也應該知道你的JSON格式不正確。在https://jsonformatter.curiousconcept.com/上查看你的json。 –

+0

我已經更新了答案,注意無效的json和JSON的標題。 –

-1

要解析JSON,你必須這樣做。

  String finalJson = buffer.toString(); 
      JSONArray parentArray = new JSONArray(finalJson); 

      for(int i=0; i<parentArray.length(); i++){ 
       JSONObject finalObject = parentArray.getJSONObject(i); 

       String title = finalObject.getString("title"); 
       String body = finalObject.getString("body"); 

       JSONArray arr = finalObject.getJSONArray("field_exercis_arc"); 
       for(int x=0; x < arr.length(); x++){ 
        String val = arr.getString(x); 
       } 
      } 
+0

謝謝,我已經改變了JSON –

+0

我認爲這應該是一個評論 – Nabin

+0

@NabinKhadka我更新了我的答案。 – Enzokie

0
JsonArray fieldArray=yourJsonObject.getJsonArray("field_exercis_arc"); 
for(int i=0;i<fieldArray.length;i++){ 
fieldArray.getString(i); 
}