JSONParse从Android Studio中的URL获取和解析数据

时间:2019-01-07 02:20:33

标签: android jsonparser

我如何拥有不同类型的JSONdata,而不是使用解析JSON复制相同的数据三次。例如,我在JSON

中解析了三种类型的数据
[
  [
  {
    "Driver name": "Tom",
    "Vehicle": "Honda Civic",
    "Status": "Learner",
    "Country": "United Kingdom"
  },
  {
    " Score Breakdown": "",
    "Acceleration": "****",
    "Speed": "***",
    "Braking": "**",
    "Cornering": "*"
  },
  {
    "Trip Time": "",
    "Start Time": "12:56",
    "End time ": "13:45",
    "Time of Day ": "Day",
    "Miles covered ": "10 miles"
  }

我在android应用程序中拥有的当前数据是此JSON格式的已解析数据,因为我正试图让三组不同的数据(如上)出现,而不是像我在下文中一样复制相同的数据。

[
  {
    "Driver name": "Tom",
    "Vehicle": "Honda Civic",
    "Status": "****",
    "Country": "United Kingdom"
  },
  {
    "Driver name": "Tom",
    "Vehicle": "Honda Civic",
    "Status": "****",
    "Country": "United Kingdom"
  },
  {
    "Driver name": "Tom",
    "Vehicle": "Honda Civic",
    "Status": "****",
    "Country": "United Kingdom"
  }
]

以下是我的android应用程序中的当前代码,可以使上面的parseJSON数据成功显示。

URL url= new URL ("https://api.myjson.com/bins/ox8fc");

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 = 1 ;i <JA.length ();i++){
    JSONObject JO = (JSONObject) JA.get(i);
    singleParsed = "Driver name" + JO.get("Driver name") + "\n " +
                   "Vehicle " + JO.get("Vehicle") + "\n " +
                   "Status " + JO.get("Status") + "\n " +
                   "Country" + JO.get("Country") + "\n " ;
    dataParsed = dataParsed + singleParsed + "\n";
}

0 个答案:

没有答案