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

1
  • Your JSOn is not validate plz check that Commented Sep 20, 2016 at 8:54

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

6 Comments

I actually tried to implement GSON before but i got some errors. Here is my post concerning GSON. stackoverflow.com/questions/39161354/… I will try again. Maybe i am lucky this time
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 jsonformatter.curiousconcept.com.
I have updated the answer with notice to invalid json and example of getting title out of JSON.
Thanks, I have changed the JSON
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.
|
0

Use below code :

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

Comments

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

Comments

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

2 Comments

Thanks, I have changed the JSON
I think this should have been a comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.