2013-09-25 92 views
0

我有類似下面的J-兒子響應,如何用android中的兩個值解析json對象?

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

我解析所有的事情,但我想在數組列表把對象值一個接一個。我不知道該怎麼做,能否有人知道幫我解決這個問題

回答

3

For Left &對,不要使用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 
    } 
} 
+0

如何我讀留下了找到正確的對象。 – Yugesh

+0

k k現在只讀了你的完整答案我得到它,我試了一下。 – Yugesh

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

然後您可以將值轉換爲字符串或JSONArray如果值是:「左」或「右」

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