Java Android Studio提取JSON问题

时间:2019-05-12 14:24:25

标签: java android

运行此代码以获取JSON时,我得到了public void onReceive(Context context, Intent intent) { NotificationViewModel viewModel = ViewModelProviders.of(XXX).get(NotificationViewModel.class); MessageNotification notification = viewModel.getNextNotification().observe(XXX, new Observer<MessageNotification>() { @Override public void onChanged(MessageNotification messageNotification) { //do stuff } }); } 。但是,当我使用时:

W/System.err: org.json.JSONException: Value

({https://api.myjson.com/bins/j5f6b),一个测试器JSON URL,它为我提供输出。

我尝试在某些地方将其更改为JSON Object,但这无济于事。

[{"name":"Abhishek","password":"123","contact":"1111111111","country":"India"},{"name":"Rahul","password":"1s","contact":"1sdfsdf","country":"India"},{"name":"Abhishek","password":"aar","contact":"asdbsfg","country":"India"}]

我希望看到一个名称输出,除了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; } JSONArray JA = new JSONArray(data); for(int i =0 ;i <JA.length(); i++){ JSONObject JO = (JSONObject) JA.get(i); singleParsed = "name:" + JO.get("name") + "\n"; dataParsed = dataParsed + singleParsed +**"\n"** ; } } 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); }

之外什么都没有

1 个答案:

答案 0 :(得分:0)

问题似乎出在您的解析逻辑上。

检查:

JSONObject jsonObject = new JSONObject(data);
JSONArray JA = jsonObject.getJSONArray("features");
for (int i = 0; i < JA.length(); i++) {
    JSONObject JO = (JSONObject) JA.get(i);
    System.out.print(JO);
}

您必须先创建一个JSONObject,然后再从中提取JSONArray。