JSONObject Android无法创建

时间:2013-01-08 19:08:50

标签: android json

我有GetRequest,作为响应,我得到了HTML头,body元素的内容是JSONS数组。我无法解析它,任何人都可以发布提示如何从这个响应创建JSON对象?

4 个答案:

答案 0 :(得分:0)

如果您在String json中有JSON返回值,则可以执行

JSONArray a = new JSONArray(json);

然后可以使用

检索数组中的第一个JSONObject
JSONObject o = a.getJSONObject(0);

答案 1 :(得分:0)

您可以从这样的字符串创建JSON对象。

String myString = "This is the response from your HTTP GET request";
JSONObject myJson = new JSONObject(myString);

请注意,myString的值必须是有效的JSON。

答案 2 :(得分:0)

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

// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);

try {
// Getting Array of Contacts
contacts = json.getJSONArray(TAG_CONTACTS);

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

    // Storing each json item in variable
    String id = c.getString(TAG_ID);
    String name = c.getString(TAG_NAME);
    String email = c.getString(TAG_EMAIL);
    String address = c.getString(TAG_ADDRESS);
    String gender = c.getString(TAG_GENDER);

    // Phone number is agin JSON Object
    JSONObject phone = c.getJSONObject(TAG_PHONE);
    String mobile = phone.getString(TAG_PHONE_MOBILE);
    String home = phone.getString(TAG_PHONE_HOME);
    String office = phone.getString(TAG_PHONE_OFFICE);

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

答案 3 :(得分:0)

我的问题是,当我从服务器下载请求时,我必须只获取元素的内容。比我使用标准的JSONArray,一切正常。

相关问题