0

I have a json response look like this

"pen": [
                {
                    "company": "Lexi",
                    "ink": "red",
                    "instock": true
                },
                {
                    "company": "Lexi",
                    "ink": "blue",
                    "instock": true
                }
            ]

But I want it in this way

"pen": [
                {
            "company": "Lexi",
                    "items":[
                {   
                    "ink": "red",
                    "instock": true
                }
                {   
                    "ink": "blue",
                    "instock": true
                }
            ]
        }
    ]

I have the POJO of the first json response.The second one holds the items with the same company name.How can I convert it to the second one?.

Kiran Benny Joseph
  • 6,755
  • 4
  • 38
  • 57

1 Answers1

0

you can try this

try {

            JSONObject jsonObject = new JSONObject(result); //result = your stream result

            String company = jsonObject.getString("pen"); //this what you got now

            JSONArray array = new JSONArray(company);

            for (int i = 0; i < array.length(); i++) {

                JSONObject jsonPart = arr.getJSONObject(i);

                Log.i("Company", jsonPart.getString("company"));


                } 
catch(Exception e)
{

}
Michael S
  • 36
  • 4
  • Sorry.Now I am terribly looking to do this project with jackson converter serialization.and also this code doesn't work too.Thanks for your response:) – Kiran Benny Joseph Oct 02 '16 at 02:11