2013-03-03 108 views
0

我想從jason webservice獲取數據獲取數據,可能無法從JSON Web服務

JSON的反應是:

{"content":[{"id":"1","asset_id":"62","title":"sample page","alias":"","introtext":"","fulltext":"Some Contents"},{"id":"2","asset_id":"62","title":"sample page2","alias":"","introtext":"","fulltext":"Some Contents"},{"id":"3","asset_id":"62","title":"sample page3","alias":"","introtext":"","fulltext":"Some Contents"}]} 

參觀Here 我已經在這樣做了之後:

private void parseData() { 
    // Creating JSON Parser instance 
    JSONParser jParser = new JSONParser(); 

    // getting JSON string from URL 
    JSONObject json = jParser.getJSONFromUrl(BASE_URL); 

    try { 
     // Getting Array of Contents 
     contents = json.getJSONArray(TAG_CONTENTS); 

     // looping through All Contents 
     for(int i = 0; i < contents.length(); i++){ 
      JSONObject c = contents.getJSONObject(i); 

      // Storing each json item in variable 
      id = c.getString(TAG_ID); 
      title = c.getString(TAG_TITLE);  
     } 

     textView.setText(id + " " + title); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
} 

現在我得到了id = 3和title = sample page3結果現在我怎麼能得到前兩個值作爲也!!?

+0

這是怎麼回事?什麼是錯誤? – 2013-03-03 08:50:38

+0

沒有任何錯誤,但結果也沒有顯示! – 2013-03-03 08:52:56

+1

你確定你沒有結束例外嗎?如果是這樣,請提供你的堆棧跟蹤,並且不要讓你的catch部分空着,你應該處理這個異常。 – Triode 2013-03-03 08:58:36

回答

1

Arshay!試試這個人!

private void parseData() { 
    // Creating JSON Parser instance 
    MyJSONParser jParser = new MyJSONParser(); 

    // getting JSON string from URL 
    JSONObject json = jParser.getJSONFromUrl(BASE_URL); 

    try { 
     // Getting Array of Contents 
     jsonArrar = json.getJSONArray(TAG_CONTENTS); 
     List list = new ArrayList<String>(); 
     // looping through All Contents 
     for(int i = 0; i < jsonArrar.length(); i++){ 
      // JSONObject c = jsonArrar.getJSONObject(i); 


      String id1=jsonArrar.getJSONObject(i).getString(TAG_ID);     
      String title=jsonArrar.getJSONObject(i).getString(TAG_TITLE);    
      String fullText=jsonArrar.getJSONObject(i).getString(TAG_FULL_TEXT);     

      list.add(id1); 
      list.add(title); 
      list.add(fullText); 
     } 

     Iterator<String> iterator = list.iterator(); 
     StringBuilder builder = new StringBuilder(); 
     while (iterator.hasNext()) { 
      String string = iterator.next();     
      builder.append(string+"\n"); 

     } 

     textView.setText(builder); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
} 
+0

謝謝!親愛的它岩石! – 2013-03-03 20:30:36

0

你的線路是JSONObject而不是JSONArray

你應該使用這樣的:

JSONObject jso = new JSONObject(line); 
JSONArray jsa = new JSONArray(jso.getJSONArray("content")); 
+0

這給了我錯誤'.getString(「id」);' – 2013-03-03 09:15:59

0

嘗試是這樣的

// jsonData:響應

名單< String>的內容=新的ArrayList <字符串>();

String [] val;

  try { 


     JSONObject jsonObj = new JSONObject(jsonData); 
     if (jsonObj.get(JSON_ROOT_KEY) instanceof JSONArray) { 

      JSONArray array = jsonObj.optJSONArray(JSON_ROOT_KEY); 
      for (int loop = 0; loop < array.length(); loop++) { 
val = new String[loop]; 
JSONObject Jsonval = array.getJSONObject(loop); 
val.Jsonval.getString(TAG_ID); 
val.Jsonval.getString(asset_id); 

。 。 etc

contents.add(val);

} 
} 

}

+0

對不起,但這並不行! – 2013-03-03 13:18:14