0

i have j-son response like below,

{
"client_id": "1",
"template_id": "4",
"scrolling_text": "scrolling Text for android",
"bottom": "scrolling Text for android",
"Left": [
    "http://www.qwerty.com/iDigitalSign/img/files/20130925043454Chrysanthemum.jpg",
    "http://www.qwerty.com/iDigitalSign/img/files/RightTulips.jpg"
],
"Right": [
    "http://www.qwerty.com/iDigitalSign/img/files/BottomKit_Kat_Dancing_Kids_TV_Commercial_-_YouTube.3gp"
]
}

My parse

InputStream in = response.getEntity().getContent();
            Json_response json_res_class = new Json_response();
            String a = json_res_class.convertStreamToString(in);


            try {
                jsonarray = new JSONArray("[" + a + "]");
                json = jsonarray.getJSONObject(0);

                String client_id = json.getString("client_id");
                String template_id = json.getString("template_id");
                scrolling_text = json.getString("scrolling_text");
                String bottom = json.getString("bottom");
                Left = json.getString("Left");
                videoPath = json.getString("Right");



            } catch (Exception e) {
                e.printStackTrace();
        }

i parse all the thing but i want put Left object value one by one in array list.I don't know how to do that thing,Can any one know help me to solve this issue

5 Answers 5

3

For Left & Right, don't use json.getString, use json.getJSONArray("Left");

Then you can iterate like this:

for (int i=0; i < jArray.length(); i++)
{
    try {
        String itemInArray = jArray.getString(i);
        // Pulling items from the array
    } catch (JSONException e) {
        // Oops
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1
{ // json object node
"client_id": "1",
"template_id": "4",
"scrolling_text": "scrolling Text for android",
"bottom": "scrolling Text for android",
"Left": [ // json array left

   try {
           JSONObject jb = new JSONObject("mysonstring");
           JSONArray jr = (JSONArray)jb.getJSONArray("Left"); 
                   // simialr for right  
           for(int i=0;i< jr.length();i++)
           {
                  String url = jr.getString(i);
                  Log.i("url.....",url);
           }

    }catch(Exception e)
    {
        e.printStackTrace();
    }

Comments

0

use this

try {
            jsonarray = new JSONArray("[" + a + "]");
            json = jsonarray.getJSONObject(0);

            String client_id = json.getString("client_id");
            String template_id = json.getString("template_id");
            scrolling_text = json.getString("scrolling_text");
            String bottom = json.getString("bottom");

            String Left[] = json.getString("Left").split(",");
            videoPath = json.getString("Right");



        } catch (Exception e) {
            e.printStackTrace();
    }

Comments

0

May be this will help you.

    try {
        JSONObject jsonObject = new JSONObject(a); //a = your Json responce string

        Map<String, Object> map = new HashMap<String, Object>();
        Iterator iter = jsonObject.keys();
        while (iter.hasNext()) {
            String key = (String) iter.next();
            Object value = jsonObject.get(key);
            map.put(key, value);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

Then you can cast the value to String or to JSONArray if the value is : "Left" or "Right"

Comments

0

U could use gson and just create a class

public class Response {
    private long id;
    private long template_id;
    private String scrolling_text;
    private String bottom;
    private String[] Left;
    private String[] Right;
}

And than just call:

Response response = new Gson().fromJson(a, Response.class);

Comments

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.