2011-11-21 105 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