2012-11-07 32 views
0

嗨,我新來JSON格式的Web服務工作。 我有JSON的喜歡....如何解析下面的JSON?

{ "meta":{"success":1}, 

"Countries": 
[[{"Id":"1","Name":"Gibraltar", 
    "Cities": 
    [[{"Id":"21","Name":"Gibraltar"}]] 
    }, 
    {"Id":"2","Name":"Canada", 
    "Cities": 
    [[{"Id":"22","Name":"Toronto"}, 
      {"Id":"39","Name":"Banff"}]] 
    }, 
     {"Id":"4","Name":"Malta", 
    "Cities": 
    [[{"Id":"37","Name":"Valletta"}]] 
    }, 
     {"Id":"51","Name":"Italy", 
    "Cities": 
    [[{"Id":"24","Name":"Sardinia"}]] 
    }, 
    {"Id":"53","Name":"England", 
    "Cities": 
    [[{"Id":"23","Name":"London"}, 
     {"Id":"38","Name":"Guildford"}, 
     {"Id":"43","Name":"Petersfield"}, 
      {"Id":"44","Name":"Isle of Wight"}]] 
    }, 
    {"Id":"175","Name":"Hungary", 
    "Cities": 
    [[{"Id":"36","Name":"Budapest"}]] 
    } 
]] 
} 

但我無法得到的值。而作爲解析JSON對象。

我曾試圖得到這樣的價值觀......

public class JSONParsing1 extends ListActivity { 

private static String url = "http://wsapi.vr2020.com/countries.json"; 

private static final String TAG_META = "meta"; 
private static final String TAG_SUCCESS = "success"; 
private static final String TAG_COUNTRIES = "Countries"; 
private static final String TAG_ID = "Id"; 
private static final String TAG_NAME = "Name"; 
private static final String TAG_CITY = "Cities"; 
private static final String TAG_CITY_ID = "Id"; 
private static final String TAG_CITY_NANE = "Name"; 

private String id; 
private String name; 
private String city_id; 
private String city_name; 
JSONObject meta; 
JSONArray Countries = null; 
JSONArray Cities = null; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String,  String>>(); 

    JSONParser jsonParser = new JSONParser(); 
    JSONObject jsonObject = jsonParser.getJSONFromUrl(url); 

    try { 
     meta = jsonObject.getJSONObject(TAG_META); 

     TextView startText = (TextView) findViewById(R.id.stat_textView); 
     startText.setText(meta.getString(TAG_SUCCESS)); 


    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    try { 

     Countries = meta.getJSONArray(TAG_COUNTRIES); 
     for (int i = 0; i < Countries.length(); i++) { 
      JSONObject obj = Countries.getJSONObject(i); 

      id = obj.getString(TAG_ID); 
      name = obj.getString(TAG_NAME); 

      JSONArray city = obj.getJSONArray(TAG_CITY); 
      for(int j = 0; j< city.length(); j++){ 
       JSONObject cityobj = city.getJSONObject(j); 

       city_id = cityobj.getString(TAG_CITY_ID); 
       city_name = cityobj.getString(TAG_CITY_ID); 

      } 

      HashMap<String, String> map = new HashMap<String, String>(); 
      map.put(TAG_ID, id); 
      map.put(TAG_NAME, name); 
      list.add(map); 
     } 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    ListAdapter adapter = new SimpleAdapter(this, list, 
      R.layout.list_items, new String[] { TAG_ID, TAG_NAME, 
        }, new int[] { R.id.id_textView, 
        R.id.name_textView, R.id.description_textView }); 
    setListAdapter(adapter); 

} 

} 

和JSON解析器類是

public class JSONParser { 

static InputStream is = null; 
static JSONObject jsonObject = null; 
static String json = ""; 

public JSONObject getJSONFromUrl(String url) { 

    try { 
     DefaultHttpClient httpClient = new DefaultHttpClient(); 
     HttpPost httpPost = new HttpPost(url); 

     HttpResponse httpResponse = httpClient.execute(httpPost); 
     HttpEntity httpEntity = httpResponse.getEntity(); 
     is = httpEntity.getContent(); 

    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    try { 
     BufferedReader reader = new BufferedReader(new InputStreamReader(
       is, "iso-8859-1"), 8); 
     StringBuilder sb = new StringBuilder(); 
     String line = null; 
     while ((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 
     is.close(); 
     json = sb.toString(); 
    } catch (Exception e) { 
     Log.e("Buffer Error", "Error converting result " + e.toString()); 
    } 

    try { 
     jsonObject = new JSONObject(json); 
    } catch (JSONException e) { 
     Log.e("JSON Parser", "Error parsing data " + e.toString()); 
    } 

    return jsonObject; 
} 
} 

任何一個可以分享如何分析上面的JSON? 在此先感謝。

+0

'但我無法得到的價值,你試過什麼? –

+0

JSON似乎很好,你的問題是什麼? – rekire

+0

遵循此鏈接..http://www.androidhive.info/2012/01/android-json-parsing-tutorial/ – Lokesh

回答

2

解析當前JSON作爲:

JSONObject jsonObj = new JSONObject(jsonStr); 

// these 2 are strings 
JSONObject c = jsonObj.getJSONObject("meta"); 
String success = c.getString("success"); 

JSONArray jsonarr = jsonObj.getJSONArray("Countries"); 

// lets loop through the JSONArray and get all the items 
for (int i = 0; i < jsonarr.length(); i++) { 
    JSONArray jsonarra = jsonarr.getJSONArray(i); 
    // lets loop through the JSONArray and get all the items 
    for (int j = 0; j < jsonarra.length(); j++) { 
     JSONObject jsonarrtwo = jsonarra.getJSONObject(j); 
       // these 2 are strings 
     String str_id = jsonarrtwo.getString("id"); 
     String str_Name = jsonarrtwo.getString("Name"); 

       JSONArray jsonarr_cities = jsonarrtwo.getJSONArray("Cities"); 
      // lets loop through the JSONArray and get all the items 
      for (int t = 0; t < jsonarr_cities.length(); t++) { 
       // printing the values to the logcat 
       JSONArray jsonarr_cities_one = jsonarrtwo.getJSONArray(t); 
      for (int tt = 0; tt < jsonarr_cities_one.length(); tt++) { 
       // printing the values to the logcat 

       JSONObject jsonarr_cities_two = jsonarr_cities_one.getJSONObject(tt); 
        // these 2 are strings 
       String str_idone = jsonarr_cities_two.getString("id"); 
       String str_Nameone = jsonarr_cities_two.getString("Name"); 
       } 
      } 
    } 
} 

解析JSON試試這個TUTS:

http://www.androidcompetencycenter.com/2009/10/json-parsing-in-android/

格式化JSON:

http://jsonviewer.stack.hu/

+0

謝謝,JSONArray jsonarr_cities_one = jsonarrtwo.getJSONArray(t);在aboveline getJSONArray(t)方法中t不能接受整數。 – Ganesh

+0

@Ganesh:嘗試'jsonArray.get(t);'而不是'jsonarrtwo.getJSONArray(t); '你可以做一些改變,但是按照你提供的方式解析json字符串是正確的。謝謝 –

0

您可以驗證您的JSON字符串由此工具http://jsonlint.com/

您的JSON字符串包含在國家標籤和城市標籤JSON數組的JSON數組。 在你的JSONParsing1活動中試試類似這樣的東西;

替換的代碼行,

Countries = meta.getJSONArray(TAG_COUNTRIES); 

與此線

Countries = new JSONArray(jsonObject.getJSONArray(TAG_COUNTRIES).toString()); 

替換的代碼行,

JSONArray city = obj.getJSONArray(TAG_CITY); 

與此線

JSONArray city = new JSONArray(obj.getJSONArray(TAG_CITY).toString()); 

它可以幫助你解決問題。嘗試這種方式...;)