2013-04-05 134 views
-8

嗨,我正好卡在有线那种problem.May的是我无法用我的心权。JSON解析正确的解决方案

我越来越喜欢

{ 
"Result":[ 
    "Entertainment", 
    "Business", 
    "Sports", 
    "Technology"] 
} 

我能够得到的第一个值I,E的结果值这样 在后执行环路响应JSON响应被

["Entertainment","Business","Sports","Technology"] 

现在问题是我得到的响应不像数组那样是字符串。那么我怎样才能分别获取这些值。 请帮助尽快

回答

0

这样的(和jsonString是从服务器源JSON响应):

JSONObject json = new JSONObject(jsonString); 
JSONArray resultArray = json.getJSONArray("Result"); 

,然后简单地遍历resultArrayget()

2

问题是,我收到响应作为字符串不是作为阵列

=>如你SA ID您有响应为字符串,那么你只需要使用你所得到的响应字符串从它使用下面创建的JSONObject,然后取JSONArray:

try{ 
    JSONObject objJSON = new JSONObject(objResponse); 
    JSONArray arrayResponse = objJSON.getJSONArray("Result"); 

    // iterate through the JSONArray and fetch one by one string 
} catch (JSONException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 
+0

@Vyas了解JSON解析[这里](http://www.technotalkative.com/android-json-parsing/)。 – 2013-04-05 10:43:44

0

您可以获取Results阵列的每个String值,从JSON字符串是这样的: -

JSONObject jsonObj = new JSONObject(jsonString); 
JSONArray resultArray = jsonObj.getJSONArray("Result"); 
for(int i=0;i<resultArray.length();i++){ 
    System.out.println(resultArray.getString(i)); // Get each String. 
} 
0

您可以分析您的JSON这样

try { 
     JSONObject obj=new JSONObject(""); 
     JSONArray arr=obj.getJSONArray("Result"); 
    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
0

尝试像

JSONObject jsonObj = new JSONObject(json); 
JSONArray resultArray = jsonObj.getJSONArray("Result"); 

for(int i=0;i<resultArray.length();i++){ 

    JSONObject jsonObj = resultArray.getJSONObject(i); 
    String Entertainment= jObject.getString("Entertainment"); 
    String Business= jObject.getString("Business"); 
    String Sports = jObject.getString("Sports"); 
    String Technology = jObject.getString("Technology "); 

} 
0

解析您的JSONArray并返回一个字符串ArrayList,试试这个:

JSONObject json = new JSONObject(jsonString); 
    JSONArray jsonArray = jsonObj.getJSONArray("Result"); 
    if(jsonArray!= null) { 
     ArrayList<String> arrayResult = new ArrayList<String>(); 
     for(int i=0;i<jsonArray.length();i++){ 
      arrayResult.add(arrayResult.getString(i)); 
     } 
    } 

注意:你应该把这个try catch集团赶上JSONException

0

我有解决了这个问题

JSONParser jParser = new JSONParser(); 
    // getting JSON string from URL 
    JSONObject json = jParser.getJSONObjectFromUrl(url); 
    Log.e("json is", "--------------"+json); 
    programResponse = json.getString("Result"); 
    Log.e("in the post-execute loop", "in the post-execute loop response is"+programResponse); 
    JSONArray arrayResponse = new JSONArray(programResponse); 
    for(int i = 0; i < arrayResponse.length(); i++){ 
    //JSONArray innerJsonArray = json.getJSONArray(i); 
    String jsonObject = arrayResponse.getString(i); 
    System.out.println(jsonObject); 
    Log.e("length is", "--------------"+jsonObject); 
    programValues.add(jsonObject); 
}