android中的json解析问题
当我将此文本解析为JSON时,遇到了一个问题。首先,我从一个URL创建了android中的json解析问题,android,json,Android,Json,当我将此文本解析为JSON时,遇到了一个问题。首先,我从一个URL创建了jsonobject。调试时我无法显示数据,有人能帮我吗 JSON: [ { "Tarih":"21.12.2015", "Imsak":"05:51", "Gunes":"07:22", "Ogle":"12:18", "Ikindi":"14:39", "Aksam":"17:02", "Yatsi"
jsonobject
。调试时我无法显示数据,有人能帮我吗
JSON:
[
{
"Tarih":"21.12.2015",
"Imsak":"05:51",
"Gunes":"07:22",
"Ogle":"12:18",
"Ikindi":"14:39",
"Aksam":"17:02",
"Yatsi":"18:26",
"Kible":"09:41"
}
]
jsonobject = JSONfunctions.getJSONfromURL("http://namazvakitleri.ahmeti.net/index.php?islem=getSehirList&ulke_id=2");
try {
// Locate the NodeList name
jsonarray = jsonobject.getJSONArray("d");
for (int i = 0; i < jsonarray.length(); i++) {
jsonobject = jsonarray.getJSONObject(i);
WorldPopulation worldpop = new WorldPopulation();
worldpop.setCountry(jsonobject.optString("SehirId"));
worldpop.setPopulation(jsonobject.optString("population"));
world.add(worldpop);
// Populate spinner with country names
worldlist.add(jsonobject.optString("SehirAdi"));
}
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
我尝试用这段代码来解决这个问题,但JSON没有正确解析:
Java代码:
[
{
"Tarih":"21.12.2015",
"Imsak":"05:51",
"Gunes":"07:22",
"Ogle":"12:18",
"Ikindi":"14:39",
"Aksam":"17:02",
"Yatsi":"18:26",
"Kible":"09:41"
}
]
jsonobject = JSONfunctions.getJSONfromURL("http://namazvakitleri.ahmeti.net/index.php?islem=getSehirList&ulke_id=2");
try {
// Locate the NodeList name
jsonarray = jsonobject.getJSONArray("d");
for (int i = 0; i < jsonarray.length(); i++) {
jsonobject = jsonarray.getJSONObject(i);
WorldPopulation worldpop = new WorldPopulation();
worldpop.setCountry(jsonobject.optString("SehirId"));
worldpop.setPopulation(jsonobject.optString("population"));
world.add(worldpop);
// Populate spinner with country names
worldlist.add(jsonobject.optString("SehirAdi"));
}
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
jsonobject=JSONfunctions.getJSONfromURL(“http://namazvakitleri.ahmeti.net/index.php?islem=getSehirList&ulke_id=2");
试一试{
//找到节点列表名称
jsonarray=jsonobject.getJSONArray(“d”);
for(int i=0;i
“[”表示JsonArray,“{”表示JsonObject。您已将结果分配给JsonObject。而不是将其分配给JsonArray
另外,我在您的回答中没有看到任何键为“d”
试试这个:
jsonarray = JSONfunctions
.getJSONfromURL("http://namazvakitleri.ahmeti.net/index.php?islem=getSehirList&ulke_id=2");
try {
for (int i = 0; i < jsonarray.length(); i++) {
jsonobject = jsonarray.getJSONObject(i);
WorldPopulation worldpop = new WorldPopulation();
worldpop.setCountry(jsonobject.optString("SehirId"));
worldpop.setPopulation(jsonobject.optString("population"));
world.add(worldpop);
// Populate spinner with country names
worldlist.add(jsonobject.optString("SehirAdi"));
}
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
jsonarray=JSONfunctions
.getJSONfromURL(“http://namazvakitleri.ahmeti.net/index.php?islem=getSehirList&ulke_id=2");
试一试{
for(int i=0;i
但请再次注意,您的响应不包含以下键:“SehirId”、“population”和“SehirAdi”“[”表示JsonArray,而“{”表示JsonObject。您已将结果分配给JsonObject。而不是将其分配给JsonArray 另外,我在您的回答中没有看到任何键为“d” 试试这个:
jsonarray = JSONfunctions
.getJSONfromURL("http://namazvakitleri.ahmeti.net/index.php?islem=getSehirList&ulke_id=2");
try {
for (int i = 0; i < jsonarray.length(); i++) {
jsonobject = jsonarray.getJSONObject(i);
WorldPopulation worldpop = new WorldPopulation();
worldpop.setCountry(jsonobject.optString("SehirId"));
worldpop.setPopulation(jsonobject.optString("population"));
world.add(worldpop);
// Populate spinner with country names
worldlist.add(jsonobject.optString("SehirAdi"));
}
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
jsonarray=JSONfunctions
.getJSONfromURL(“http://namazvakitleri.ahmeti.net/index.php?islem=getSehirList&ulke_id=2");
试一试{
for(int i=0;i
但请再次注意,您的响应不包含此键“SehirId”、“population”和“SehirAdi”您的响应是
JsonArray
而不是JsonObject
现在你有两种方法来解决这个问题
getJSONfromURL()
以返回JsonArray
JsonObject
而不是JsonArray
注意:您有可用的catch块,因此请尝试检查logcat中的错误并在google上搜索与该错误相关的内容。您的回答是
JsonArray
而不是JsonObject
现在你有两种方法来解决这个问题
getJSONfromURL()
以返回JsonArray
JsonObject
而不是JsonArray
try {
JSONArray jsonArray = new JSONArray("Your Response");
JSONObject jsonObject = (JSONObject) jsonArray.get(0);
Iterator keyNames = jsonObject.keys();
while (keyNames.hasNext()) {
String keyName = (String) keyNames.next();
String keyValue = jsonObject.getString(keyName);
}
} catch (Exception e) {
e.printStackTrace();
}
尝试以下解决方案:
try {
JSONArray jsonArray = new JSONArray("Your Response");
JSONObject jsonObject = (JSONObject) jsonArray.get(0);
Iterator keyNames = jsonObject.keys();
while (keyNames.hasNext()) {
String keyName = (String) keyNames.next();
String keyValue = jsonObject.getString(keyName);
}
} catch (Exception e) {
e.printStackTrace();
}
发布完整的json,getJSONArray(“d”)是什么意思;在代码中,您的响应是
JsonArray
而不是JsonObject
@Diego:r得到错误的json响应。响应应该在{和}之内。发布getJSONfromURL方法发布完整的json,getJSONArray(“d”)是什么意思;在你的代码中,你的响应是JsonArray
而不是JsonObject
@Diego:r得到错误的Json响应。响应应该在{和}之内。发布你的getJSONfromURL方法我尝试过这个方法,但不起作用..JsonArray=JsonObject.getJSONArray(“);我遇到一个错误:类型不匹配:无法从JSONObject转换为JSONArrayit意味着您正在从“JSONfunctions.getJSONfromURL()获取JSONObject作为响应'方法。但是您发布了一个Json,它是数组。.您能再次验证并发布您得到的Json响应吗?当我尝试解析您的url请求时,我得到了一个响应。这是您期望的响应吗?:[{“Disabled”:false,“Group”:null,“SehirAdi”:“ADANA”,“SehirId”:“500”},{“Disabled”:false,“Group”:null,“SehirAdi”:“ADIYAMAN”,“SehirId:“501”},{“Disabled”:false,“Group”:null,“SehirAdi:“AFYON”,“SehirId:“502”}]很高兴听到这个消息。愉快的编码我尝试了这个方法,但没有成功..jsonarray=jsonobject.getJSONArray(“);我遇到一个错误:类型不匹配:无法从JSONObject转换为JSONArrayit意味着您正在从“JSONfunctions.getJSONfromURL()获取JSONObject作为响应