有人能帮我吗?我在尝试解析此API时似乎无法修复错误:
{
"createDate": 1321834118923,
"result": "SUCCESS",
"searchTerm": "N7",
"searchableLocations": [
{
"identifier": "OUTCODE^1685",
"name": "N7"
}
]
}请帮我找出问题
package com.somcollection;
import android.app.Activity;
import android.os.Bundle;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
public class JsonParser extends Activity {
private JSONObject jObject;
private String jString = "{\"searchTerm\":\"N7\",\"searchableLocations
\":[{\"identifier\":\"OUTCODE^1685\",\"name\":\"N7\"}],\"createDate
\":1321834118923,\"result\":\"SUCCESS\"}";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
parse();
} catch (Exception e) {
e.printStackTrace();
}
}
private void parse() throws Exception {
jObject = new JSONObject(jString);
JSONObject jObject = new JSONObject(jString);
String menuObject = jObject.getString("searchTerm");
JSONArray menuitemArray = jObject.getJSONArray("searchableLocations");
for (int i = 0; i < menuitemArray.length(); i++) {
JSONObject jsonObj = menuitemArray.getJSONObject(i);
String attributeId = jsonObj.getString("identifier");
System.out.println(attributeId);
String attributeValue = jsonObj.getString("name");
System.out.println(attributeValue);
}
String createDate = jObject.getString("jObject");
String result = jObject.getString("result");
}
}发布于 2011-11-22 05:52:15
下面的代码行将失败:
String createDate = jObject.getString("jObject");因为在名为"jObject“JSON中没有键。我希望你能写下
String createDate = jObject.getString("createDate");顺便说一下,您可能也不想对这个值使用getString(),因为从技术上讲,它在JSON中定义为一个整数。
https://stackoverflow.com/questions/8218966
复制相似问题