gpt4 book ai didi

java - 未找到名称正在获取 JSON,Android

转载 作者:行者123 更新时间:2023-12-02 05:45:14 26 4
gpt4 key购买 nike

运行此代码获取 JSON 时,我收到“org.json.JSONException: No value for name”。但是,当我使用:( https://api.myjson.com/bins/j5f6b )(测试器 JSON URL)时,它会为我提供所需的输出。

已尝试在某些地方将其更改为 JSON 对象,但这没有帮助。

public class fetchData extends AsyncTask<Void,Void,Void> {
String data ="";
String dataParsed = "";
String singleParsed ="";
@Override
protected Void doInBackground(Void... voids) {
try {
URL url = new URL("http://www.free-map.org.uk/fm/ws/bsvr.php?bbox=-0.73,51.04,-0.71,51.06&way=highway&format=json");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
while(line != null){
line = bufferedReader.readLine();
data = data + line;
}

JSONObject jsonObject = new JSONObject(data);
JSONArray JA = jsonObject.getJSONArray("features");
for (int i = 0; i < JA.length(); i++) {
JSONObject JO = (JSONObject) JA.get(i);
singleParsed = "name:" + JO.get("name") + "\n";
dataParsed = dataParsed + singleParsed +"\n" ;

System.out.print(JO);
}



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

return null;
}


protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);

twod.data.setText(this.dataParsed);

}

预期结果:名称输出实际结果:org.json.JSONException:名称没有值

最佳答案

属性名称位于属性对象内部。因此,要访问名称,首先获取属性 JSON 对象并使用它。

    JSONArray JA = jsonObject.getJSONArray("features");
for (int i = 0; i < JA.length(); i++) {
JSONObject JO = (JSONObject) JA.get(i);
singleParsed = "name:" + JO.getJSONObject("properties").get("name") + "\n"; // add .getJSONObject("properties")
dataParsed = dataParsed + singleParsed +"\n" ;

System.out.print(JO);
}

关于java - 未找到名称正在获取 JSON,Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56101160/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com