2013-06-05 198 views
1

我試圖解析以下JSONString如何解析JSON字符串的Android

[[{"0":" 
","title":" Technical Support Analyst in Noida","1":" 
","Company Name":" Oracle","2":" 
","Category":"Fresher","3":" 
","Job Type":"Full Time","4":" 
","Location":"Noida","5":" 
","Job Qualification":"BE\/BTch\/Bsc\/Others","6":" 
","Job Experience":"Freshers","7":" 
","Job postdate":"2013-6-05","8":" 
"}]] 

這裏我的代碼:

// try parse the string to a JSON object 
try { 
    //jObj = new JSONObject(JsonString); 
    JSONArray ja = new JSONArray(result); 
    int size = ja.length(); 
    Log.d("tag", "No of Elements " + ja.length()); 
} catch (JSONException e) { 
    Log.e("JSON Parser", "Error parsing data " + e.toString()); 
} 

可以在任何一個幫助,我的代碼不能正常工作? 我想分析標題,公司名稱,類別等等...

+0

參閱此http:// stackoverflow.com/questions/9605913/how-to-parse-json-in-android – Riser

+3

字符串不是一個有效的json – Blackbelt

+0

http://stackoverflow.com/questions/8091051/how-to-parse-json-string-in-android和http://stackoverflow.com/questions/3408985/json-array -iteration-in-android-java – Sam

回答

1

您需要從jsonstring創建JSONArray

你有JSONArrayJSONArray然後JSONObect ..

try { 
     JSONArray ja = new JSONArray(buffer.toString()); 
     JSONArray innerJsonArray = ja.getJsonArray(0); 
     JSONObject object = innerJsonArray.getJSONObject(0); 
     String title = object.getString("title");     
    } 
    catch (JSONException e) { 
     Log.e("JSON Parser", "Error parsing data " + e.toString()); 
    } 
1

看看這個JSON解析指南使用我寫的本地工具和GSON庫:

Json Parsing

也許你會發現它有用。 您可以從那裏下載完整的項目以便自己運行和測試。

1

你需要構建你的json。

沒有名爲「result」的數組。你必須做的是用一個唯一的名字命名json的每個元素,以便獲取它。

{"result": 
    ["result1":["result2":{"0":" 
    ","title":" Technical Support Analyst in Noida","1":" 
    ","Company Name":" Oracle","2":" 
    ","Category":"Fresher","3":" 
    ","Job Type":"Full Time","4":" 
    ","Location":"Noida","5":" 
    ","Job Qualification":"BE\/BTch\/Bsc\/Others","6":" 
    ","Job Experience":"Freshers","7":" 
    ","Job postdate":"2013-6-05","8":" 
    "}]]} 
+0

你在哪裏讀過這些內容? – NaserShaikh

1

你可以試試下面的代碼解析JSON

{ 
"result": "success", 
"countryCodeList": 
[ 
{"countryCode":"00","countryName":"World Wide"}, 
{"countryCode":"kr","countryName":"Korea, Republic of"}, 
{"countryCode":"us","countryName":"United States"}, 
{"countryCode":"jp","countryName":"Japan"}, 
{"countryCode":"cn","countryName":"China"}, 
{"countryCode":"in","countryName":"India"} 
] 
} 

解析代碼

public static ArrayList<Country> ParseJson(String jsonstring) { 

    ArrayList<Country> arrCountries = new ArrayList<Country>(); 

    String status; 
    String message = ""; 
    try { 


     JSONObject json = new JSONObject(jsonstring); 

     status = json.getString("result"); 

     if (status.equalsIgnoreCase("success")) { 


      JSONArray nameArray = json.names(); 
      JSONArray valArray = json.toJSONArray(nameArray); 

      JSONArray valArray1 = valArray.getJSONArray(1); 

      valArray1.toString().replace("[", ""); 
      valArray1.toString().replace("]", ""); 

      int len = valArray1.length(); 

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

       Country country = new Country(); 
       JSONObject arr = valArray1.getJSONObject(i); 

       country.setCountryCode(arr.getString("countryCode")); 
       country.setCountryName(arr.getString("countryName")); 
       arrCountries.add(country); 
      } 
     } 

    } catch (JSONException e) { 
     Log.e("JSON", "There was an error parsing the JSON", e); 
    } 
    return arrCountries; 
}