2012-08-28 27 views
-3

可能重複:
How can I parse this JSON in Android?解析在Android的這個JSON

我下面這個link解析的json,但我無法執行,因爲我很困惑。甚至連重複鏈接都沒有提供任何參考,那些投票結束這個問題的用戶可以在他們提供的LINK沒有提供任何幫助來解決這些問題時回答我的問題?

我認爲那些無法回答問題的人沒有權利編輯某人的問題,也沒有權利在他們提供的鏈接沒有提供任何解決方法時解決問題。 幫我解析數據。

[ 
{ 
"id": "c200", 
"name": "XYZ", 
"email": "[email protected]", 
}, 
{ 
"id": "c201", 
"name": "Johnny", 
"email": "[email protected]", 

}] 

【答案】

因爲它是一個封閉的職位,所以我張貼我的答案的問題:

//JSONArray 
JSONArray hospital = null; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    // Hashmap for ListView 
    ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>(); 

    // Creating JSON Parser instance 
    JSONParser jParser = new JSONParser(); 

    // getting JSON string from URL 
    String json = jParser.makeHttpRequest(url, "GET", 
      contactList); 

    try { 
     // Getting Array of Contacts 
      hospital = new JSONArray(json); 

      if (hospital != null) { 

      // looping through 
       for (int i = 0; i < hospital.length(); i++) { 
        JSONObject contacts = hospital.getJSONObject(i); 

      // Storing each json item in variable 
      String id = contacts.getString(TAG_HID); 
      String name = contacts.getString(TAG_HNAME); 
      String address = contacts.getString(TAG_HADDRESS); 
      String phone1=contacts.getString(TAG_HPHONE1); 
      String phone2=contacts.getString(TAG_HPHONE2); 
      String fax=contacts.getString(TAG_HFAX); 
      String email=contacts.getString(TAG_HEMAIL); 
      String url=contacts.getString(TAG_URL); 
      String hlongitude=contacts.getString(TAG_LONGITUDE); 
      String hlatitude=contacts.getString(TAG_LATITUDE); 
      String hdistance=contacts.getString(TAG_HDISTANCE); 


      // creating new HashMap 
      HashMap<String, String> map = new HashMap<String, String>(); 

      // adding each child node to HashMap key => value 
      map.put(TAG_HID, id); 
      map.put(TAG_HNAME, name); 
      map.put(TAG_HADDRESS, address); 
      map.put(TAG_HPHONE1, phone1); 
      map.put(TAG_HPHONE2, phone2); 
      map.put(TAG_HFAX, fax); 
      map.put(TAG_HEMAIL, email); 
      map.put(TAG_URL, url); 
      map.put(TAG_LONGITUDE, hlongitude); 
      map.put(TAG_LATITUDE, hlatitude); 
      map.put(TAG_HDISTANCE, hdistance); 

      // adding HashList to ArrayList 
      contactList.add(map); 
     } 
    } 
    } 
    catch (JSONException e) { 
     e.printStackTrace(); 
    } 

一些編碼爲JSON解析器類

public class JSONParser { 

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

// constructor 
public JSONParser() { 

} 

// function get json from url 
// by making HTTP POST or GET method 
public String makeHttpRequest(String url, String method, 
     ArrayList<HashMap<String, String>> contactList) { 

    // Making HTTP request 
    try { 

     // check for request method 
     if (method == "POST") { 
      // request method is POST 
      // defaultHttpClient 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      HttpPost httpPost = new HttpPost(url); 
      httpPost.setEntity(new UrlEncodedFormEntity((List<? extends NameValuePair>) contactList)); 

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

     } 
     else if (method == "GET") 
     { 
      // request method is GET 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      String paramString = URLEncodedUtils.format((List<? extends NameValuePair>) contactList, "utf-8"); 
      url += "?" + paramString; 
      HttpGet httpGet = new HttpGet(url); 

      HttpResponse httpResponse = httpClient.execute(httpGet); 
      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()); 
    } 

    // return JSON String 
    return json; 

} 
} 
+3

只顯示你試過的東西,你想要什麼?像這樣你可以問.. –

回答

0

既然你沒有說,我假設你正在使用內置組織Android附帶的.json解析器。如果您使用的是其他解析器,請查閱其文檔。

將json作爲字符串傳遞給JSONArray constructor