如何用android中的两个值解析json对象?

时间:2013-09-25 06:44:47

标签: android json

我有如下的j-son响应,

{
"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"
]
}

我的解析

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

我解析所有的东西,但我想在数组列表中逐个放置对象值。我不知道该怎么做,任何人都知道帮我解决这个问题

5 个答案:

答案 0 :(得分:3)

左派&是的,不要使用json.getString,使用json.getJSONArray(“Left”);

然后你可以像这样迭代:

for (int i=0; i < jArray.length(); i++)
{
    try {
        String itemInArray = jArray.getString(i);
        // Pulling items from the array
    } catch (JSONException e) {
        // Oops
    }
}

答案 1 :(得分: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();
    }

答案 2 :(得分:0)

使用此

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

答案 3 :(得分:0)

可能会对你有帮助。

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

然后,如果值为:“Left”或“Right”,则可以将值转换为String或JSONArray

答案 4 :(得分:0)

你可以使用gson并只创建一个类

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

而不仅仅是打电话:

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