1

I have JSON that I want to combine data from 1st object into 2nd object

{
    "ViewId": {
        "56": {
            "ViewId": "56",
            "Name": "hi",
            "param": "value"
        },
        "88": {
            "ViewId": "88",
            "Name": "hi2",
            "param": "value2"
        }
    },
    "que": [
        {
            "RId": "123",
            "ViewId": "88",
            "Count": 0
        },
        {
            "RId": "456",
            "ViewId": "56",
            "Count": 0
        }
    ]
}

Basically from this I am making ArrayList, How can I add ViewId data in to que. I want to merge JSON in following way:

{
    "que": [
        {
            "RId": "123",
            "ViewId": "88",
            "Name": "hi2",
            "param": "value2",
            "Count": 0
        },
        {
            "RId": "456",
            "ViewId": "56",
            "Name": "hi",
            "param": "value",
            "Count": 0
        }
    ]
}
6
  • It is not clear what do you want. Please try to make your question more correctly. Use commas and Capital letters please. Commented Apr 25, 2016 at 7:20
  • You can run a loop for that and check for respective JSONObject in the viewId array and modify that. Commented Apr 25, 2016 at 7:20
  • Do you want to merge elements of viewId into que? like 1st element of viewId should be merged into que's first element, right? Commented Apr 25, 2016 at 7:22
  • 1
    yes i want merge but as per der ViewId like ViewId of 88 should add into que whose view id is 88 Commented Apr 25, 2016 at 7:33
  • Can you add information about what JSON library you are using? (if any?) Commented Apr 25, 2016 at 7:47

2 Answers 2

1
    JSONObject ViewIdJsnObject = new JSONObject(); //replace new JSONObject() with ViewId Json Object here
    JSONArray queArray = new JSONArray();//replace new JSONArray() with actual json array;

    //Traverse through all que objects in array
    if(queArray != null && queArray.length() > 0){
        for(int i=0; i<queArray.length(); i++){
            try {
                JSONObject queObj = queArray.getJSONObject(i);
                String queViewId = queObj.getString("ViewId"); //ViewId of que object at position i
                JSONObject viewIdObj = ViewIdJsnObject.getJSONObject(queViewId); //get json object against ViewId
                if(viewIdObj != null) {
                    //Now add these value to que object at position i
                    String name = viewIdObj.getString("Name");
                    String param = viewIdObj.getString("param");
                    queObj.put("Name", name);
                    queObj.put("param", param);
                }
            } catch (JSONException jse) {
                jse.printStackTrace();
            }
        }
    }
    //Now que array contains final merged data, convert it to ArrayList<Your_model>.
Sign up to request clarification or add additional context in comments.

3 Comments

queObj is not adding it will always overlap the data
You should add it to your array list directly then. Add que object first then add corresponding name & param values
JSONArray finalValue = new JSONArray(); and then adding to finalValue.put(queObj) in for loop. finalValue will have all data
1

Make A class

public class Data {
    int id;
    List<Que> que = new ArrayList<Que>();


    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public List<Que> getQue() {
        return que;
    }
    public void setQue(List<Que> que) {
        this.que = que;
    }

}

Make Another class called Que

public class Que {
    int RId;
    int ViewId;
    int Count;


    public int getrId() {
        return RId;
    }
    public void setrId(int rId) {
        this.RId = rId;
    }
    public int getViewId() {
        return ViewId;
    }
    public void setViewId(int viewId) {
        this.ViewId = viewId;
    }
    public int getCount() {
        return Count;
    }
    public void setCount(int count) {
        this.Count = count;
    }

}

Use it using gson

Gson gson = new Gson();
        Data data = gson.fromJson(json, Data.class);
        List<Que> queList = data.getQue();
        for(Que que : queList){
            System.out.println("This is R ID" +que.RId);
            System.out.println("This is View ID" +que.ViewId);
            System.out.println("This is Count" +que.Count);

Make sure your json attributes name matches the java instance parameters.

1 Comment

@andro This will work. I am sorry, I was lazy. You have to figure out for other json fields like above "que" which is "ViewId": "56": { .... },....... },

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.