2011-11-21 143 views
0

我已經嘗試了幾個使用JSON解析的教程,但我似乎無法讓它們工作。請任何人都能幫忙,並指導我走向正確的方向?Android JSON API解析

我現在已經在一些有幫助的人的幫助下對我之前的課進行了一些更改,現在我得到的錯誤更少了。請幫忙。

package com.somcollection; 

import android.app.Activity; 
import android.os.Bundle; 

import org.json.JSONArray; 
import org.json.JSONObject; 
import android.app.Activity; 
import android.os.Bundle; 

public class JsonParser extends Activity { 
    private JSONObject jObject; 
    private String jString = "{\"searchTerm\":\"N7\",\"searchableLocations\":[{\"identifier\":\"OUTCODE^1685\",\"name\":\"N7\"}],\"createDate\":1321834118923,\"result\":\"SUCCESS\"}"; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     try { 
      parse(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    private void parse() throws Exception { 
     jObject = new JSONObject(jString); 
     JSONObject jObject = new JSONObject(jString); 
     String menuObject = jObject.getString("searchTerm"); 
     JSONArray menuitemArray = jObject.getJSONArray("searchableLocations"); 
     for (int i = 0; i < menuitemArray.length(); i++) { 
      JSONObject jsonObj = menuitemArray.getJSONObject(i); 
      String attributeId = jsonObj.getString("identifier"); 
      System.out.println(attributeId); 
      String attributeValue = jsonObj.getString("name"); 
      System.out.println(attributeValue); 
     } 
     String createDate = jObject.getString("jObject"); 
     String result = jObject.getString("result"); 
    } 
} 
+1

它爲什麼不起作用?有沒有錯誤信息?你的預期和實際產出是多少? – fredley

+0

'for(inti = 0i Dalmas

+0

爲什麼不能正常工作?您的問題到底是什麼,您需要準確嗎? –

回答

1

Json對象可能包含多個鍵值對。在你的問題發佈的jString中,「searchTerm」,「searchableLocations」等字符串是String鍵。這些值可以是任何類似於String,Int,Bollean,JSonArray等的對應於「searchableLocations」關鍵字的值是JsonArray(由[]表示)。

JSONObject jObject = new JSONObject(jString);  
    String menuObject = jObject.getString("searchTerm");  
    JSONArray menuitemArray = jObject.getJSONArray("searchableLocations"); 

    for (int i = 0; i < menuitemArray.length(); i++) { 
     JSONObject jsonObj = menuitemArray.getJSONObject(i);  
     String attributeId = jsonObj.getString("identifier");  
     System.out.println(attributeId);  

     String attributeValue = jsonObj.getString("name");  
     System.out.println(attributeValue); 
    } 

    String createDate = jObject.getString("jObject"); 
    String result = jObject.getString("result"); 
+0

Hello Kartik/Amy,我認爲你的解決方案將工作,但目前我收到錯誤。這些錯誤是11-21 10:40:39.687:D/AndroidRuntime(447):關閉虛擬機11-21 10:40:39.687:W/dalvikvm(447):threadid = 1:線程退出與未捕獲的異常(組= 0x4001d800)11-21 10:40:39.826:E/AndroidRuntime(447):並說到底,android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577) 21 12:47:49.946:E/AndroidRuntime(568):... 11更多11-21 12:47:55.286:I /進程(568):發送信號。 PID:568 SIG:9 – Mohamed

+0

嘿傢伙PLz告訴我我做錯了什麼 – Mohamed

+0

代碼中是否有任何特定的行是錯誤發生?只有在調用parse()方法時纔會發生這種情況嗎? – Ian

0

這段代碼解決問題了嗎?

Gson gson = new Gson(); 
MyClass obj = gson.fromJson(jString ,MyClass.class); 
+0

我不確定它會如何幫助,請你能向我解釋它是如何工作的 – Mohamed