【问题标题】:Parsing JSON array of objects in android在android中解析对象的JSON数组
【发布时间】:2016-08-09 08:07:37
【问题描述】:

我想从这个 api 调用https://api.tfl.gov.uk/Line/25,86,w19/Arrivals?stopPointId=490009219W&app_id=&app_key= 中获取 lineId、destinationName 和 timeToStation 有人可以帮忙举个例子吗? [ { "$type": "Tfl.Api.Presentation.Entities.Prediction, Tfl.Api.Presentation.Entities", "id": "-480785385", "operationType": 1, "vehicleId": "BJ11DSX", "naptanId": "490009219W", "stationName": "Little Ilford Lane", "lineId": "25", "lineName": "25", "platformName": "B", "direction": "inbound", "bearing": "245", "destinationNaptanId": "", "destinationName": "Oxford Circus", "timestamp": "2016-04-17T16:56:56.463Z", "timeToStation": 1534, "currentLocation": "", "towards": "East Ham or Manor Park", "expectedArrival": "2016-04-17T17:22:31Z", "timeToLive": "2016-04-17T17:23:01Z", "modeName": "bus" }, { "$type": "Tfl.Api.Presentation.Entities.Prediction, Tfl.Api.Presentation.Entities", "id": "1992301652", "operationType": 1, "vehicleId": "BJ11DVA", "naptanId": "490009219W", "stationName": "Little Ilford Lane", "lineId": "25", "lineName": "25", "platformName": "B", "direction": "inbound", "bearing": "245", "destinationNaptanId": "", "destinationName": "Oxford Circus", "timestamp": "2016-04-17T16:56:56.463Z", "timeToStation": 1159, "currentLocation": "", "towards": "East Ham or Manor Park", "expectedArrival": "2016-04-17T17:16:16Z", "timeToLive": "2016-04-17T17:16:46Z", "modeName": "bus" }, { "$type": "Tfl.Api.Presentation.Entities.Prediction, Tfl.Api.Presentation.Entities", "id": "733078946", "operationType": 1, "vehicleId": "BJ11DVG", "naptanId": "490009219W", "stationName": "Little Ilford Lane", "lineId": "25", "lineName": "25", "platformName": "B", "direction": "inbound", "bearing": "245", "destinationNaptanId": "", "destinationName": "Oxford Circus", "timestamp": "2016-04-17T16:56:56.463Z", "timeToStation": 790, "currentLocation": "", "towards": "East Ham or Manor Park", "expectedArrival": "2016-04-17T17:10:07Z", "timeToLive": "2016-04-17T17:10:37Z", "modeName": "bus" } ]

我的 AsyncTask 在下面给出

   @Override
    protected JSONObject doInBackground(String... args){
       JSONParser jsonParser = new JSONParser();

        Log.i("URL", url);
        JSONObject json = jsonParser.getJSONFromUrl(url);
        if(json == null) {
            Log.i("Json obj =" , "NULL");
        }
        else{
            return json;
        }
        return new JSONObject();
    }

    @Override
    protected void onPostExecute(JSONObject json){
        progressDialog.dismiss();
        //String shopName ="";
       // String distance="";

        try{
            //Fetching JSON Array
            Log.i("JSON", json.toString());

            jsonData = new JSONArray(json);

            Double arrivalTime= 0.0;

            for(int i=0;i<json.length();i++) {

                try {
                    JSONObject c = json.getJSONObject(i);

                    busNoArray.add(json.getString(TAG_LINEID));
                    destinationArray.add(json.getString(TAG_DESTINATION));

                    arrivalTime = json.getDouble(TAG_TIME) / 60;
                    arrivalTimeArray.add(arrivalTime);

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

            }

            adapter = new BusTimeAdapter(BusTimeActivity.this,busNoArray, destinationArray, arrivalTimeArray);
            mListView.setAdapter(adapter);`

有人可以帮我吗?

【问题讨论】:

  • 你确定这是你的 json Log.i("JSON", json.toString()); 记录吗?你做错了一切。
  • 这只是为了确保它不是空响应。我不明白的是如何访问数组内的对象,因为我的 doInBackground 返回一个对象,我不确定如何访问数组内的值。
  • 我已将其添加为答案,我使用 0 仅检索第一个对象,但您也可以循环检索其他对象。并添加了一些 cmets 来澄清这些。希望对您有所帮助。如果您有任何困惑,请随时询问。

标签: java android json api


【解决方案1】:

首先确保您已检索到 JSON 字符串,

然后你就可以很容易的访问它们里面的属性了

try {
     JSONArray jsonArray = new JSONArray(json);
     JSONObject obj = jsonArray.getJSONObject(0); //0 for just retrieving first object you can loop it
     String myVehicleID = obj.getString("vehicleId"); //To retrieve vehicleId
     //Similarly do it for others as well
    } catch (JSONException e) {
      e.printStackTrace();
    }

【讨论】:

  • JSONArray = new JSONArray(json); 说调用需要 API 级别 19。我的最低 API 是 9,所以我将您的代码修改为 JSONArray = new JSONArray(json.toString());,但它给了我运行时错误 E/JSON Parser: Error parsing dataorg.json.JSONException: 任何想法?
  • 是的,您需要将字符串作为参数传递给new JSONArray()。这意味着您没有得到有效的 json。请记录您的json.toString() 并粘贴输出。
猜你喜欢
  • 1970-01-01
  • 2015-04-28
  • 2016-11-20
  • 1970-01-01
  • 2015-06-13
  • 2011-08-04
  • 1970-01-01
  • 2015-06-10
  • 2013-09-29
相关资源
最近更新 更多