【发布时间】:2011-07-30 19:33:40
【问题描述】:
我在从 JSON 对象中提取值时遇到一些问题。这是我的代码
try {
JSONObject json = new JSONObject(result);
JSONObject json2 = json.getJSONObject("results");
test = json2.getString("name");
} catch (JSONException e) {
e.printStackTrace();
}
test 被声明为String。代码运行时显示null。如果我在调试模式下将鼠标悬停在 json2 上,我可以看到对象中的所有值和名称。
我也试过了
test = json2.length();
这返回了test = 0。即使我将鼠标悬停在json2 对象上,我也可以读取对象中的值。
这是我将使用的 JSON 字符串示例。
{
"caller":"getPoiById",
"results":
{
"indexForPhone":0,
"indexForEmail":"NULL",
"indexForHomePage":"NULL",
"indexForComment":"NULL",
"phone":"05137-930 68",
"cleanPhone":"0513793068",
"internetAccess":"2",
"overnightStay":"2",
"wasteDisposal":"2",
"toilet":"2",
"electricity":"2",
"cran":"2",
"slipway":"2",
"camping":"2",
"freshWater":"2",
"fieldNamesWithValue":["phone"],
"fieldNameTranslations": ["Telefon"],
"id":"1470",
"name":"Marina Rasche Werft GmbH & Co. KG",
"latitude":"52.3956107286487",
"longitude":"9.56583023071289"
}
}
【问题讨论】:
-
您提供的 JSON 字符串示例至少对我有用。您是否也遇到了该字符串的问题?
-
你能显示整个代码吗?或者至少帮我解决我的jsonobject问题