0

I am able to parse everything i need, except for the target_id's in the field_exercis_arc.
I get the nid, title and body. Not sure how to get the id's in the field_exercis_arc.


The JSON

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

The Code

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

The model for the field_exercis_arc and target_id's fields

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

Thanks in advance

Charm Geld
  • 191
  • 2
  • 17

4 Answers4

1

I recommend you to use GSON library to get result from JSON. For that you will need Java class in order to parse result to object. For this you can use JSON to Java Class conversion here.

For you example classes would be:

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

You can convert result to RootObject. Fox example:

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

Result is:

Title is: Question test

After this you can use your object to get any value from your JSON.

Also you should know that your JSON is not valid. You have commas on two places that should not exists. In string i gave you above those are fixed. You should check you JSON with: JSON Formatter

Vuk Vasić
  • 1,398
  • 10
  • 27
  • I actually tried to implement GSON before but i got some errors. Here is my post concerning GSON. http://stackoverflow.com/questions/39161354/gson-array-error-message-expected-a-string-but-was-begin-array I will try again. Maybe i am lucky this time – Charm Geld Sep 20 '16 at 09:03
  • On this link i see that you are also using JSONArray and then you are iterating trough array and then using Gson. This is not good. You should immediately convert json to object using Gson just like i give you an example in previous answer. Also you should know that you JSON is not well formatted. Check your json on https://jsonformatter.curiousconcept.com/. – Vuk Vasić Sep 20 '16 at 09:07
  • I have updated the answer with notice to invalid json and example of getting title out of JSON. – Vuk Vasić Sep 20 '16 at 09:14
  • Thanks, I have changed the JSON – Charm Geld Sep 20 '16 at 09:19
  • I have also gived response to your other GSON question with example. You need to use TypeToken when getting a List of objects with GSON. – Vuk Vasić Sep 20 '16 at 09:28
  • Thanks Vuk, I am going to try and implement GSON again. I wanted to give you an up vote but my reputation is too low. I promise to give you an upvote as soon as my reputation improves. – Charm Geld Sep 20 '16 at 10:43
0

Use below code :

exercis.setTarget_id(finalObject.getJSONArray("field_exercis_arc").getString(j));
Asif Patel
  • 1,744
  • 1
  • 20
  • 27
0
JsonArray fieldArray=yourJsonObject.getJsonArray("field_exercis_arc");
for(int i=0;i<fieldArray.length;i++){
 fieldArray.getString(i);
}
Kona Suresh
  • 1,836
  • 1
  • 15
  • 25
-1

TO the parse the JSON you have to do it like this.

            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);
                }
            }
Enzokie
  • 7,365
  • 6
  • 33
  • 39