-1

我在解析 JSON 值时遇到了一些问题,请在下面找到 JSON 文件,

  {
       "Account": "xxx",
       "Account_desc": "xxx",
       "TimeZone": "GMT+05:30",
       "DeviceList": [
          {
             "Device": "xttt",
             "Device_desc": "txtx",
             "EventData": [
                {
                   "Device": "xttt",
                   "Timestamp": 1373539125,
                   "Timestamp_date": "2013/07/11",
                   "Timestamp_time": "16:08:45",
                   "StatusCode": 61715,
                   "StatusCode_hex": "0xF113",
                   "StatusCode_desc": "Stop",
                   "GPSPoint": "12.97887,77.51030",
                   "GPSPoint_lat": 12.97887,
                   "GPSPoint_lon": 77.51030,
                   "Speed": 0.0,
                   "Speed_units": "km/h",
                   "Odometer": 3.416,
                   "Odometer_units": "Km",
                   "Geozone": "zone4",
                   "Geozone_index": 0,
                   "Address": "cxcxc",
                   "DigitalInputMask": 251,
                   "DigitalInputMask_hex": "0xFB",
                   "Index": 0
                }
             ]
          },
          {
             "Device": "pppp",
             "Device_desc": "statstr",
             "EventData": [
                {
                   "Device": "pppp",
                   "Timestamp": 1368870217,
                   "Timestamp_date": "2013/05/18",
                   "Timestamp_time": "15:13:37",
                   "StatusCode": 61715,
                   "StatusCode_hex": "0xF113",
                   "StatusCode_desc": "Stop",
                   "GPSPoint": "14.26281,80.11421",
                   "GPSPoint_lat": 14.26281,
                   "GPSPoint_lon": 80.11421,
                   "Speed": 0.0,
                   "Speed_units": "km/h",
                   "Odometer": 373.874,
                   "Odometer_units": "Km",
                   "Geozone": "port",
                   "Geozone_index": 0,
                   "Address": "asdfsdfss",
                   "Index": 0
                }
             ]
          }
       ]
    }

从上面的 JSON 中,我想在“ EventData ”中只有“GPSPoint_lat”、“GPSPoint_lon”和“Device ”所以我做了如下编码,

JSONObject jsonObject = ApplicationContext.getHttpService()
                    .readAsJson(s);
            // System.out.println("indexvalue:"+s.indexOf(1,5));
            // JSONObject object = jsonObject.getJSONObject("DeviceList");
            JSONArray jsonArray = jsonObject.getJSONArray("DeviceList");

            JSONObject object = jsonArray.getJSONObject(0);

            JSONArray secondarray = object.getJSONArray("EventData");

for (int i = 0; i < secondarray.length(); i++) {
                try {
                    System.out.println("Im...");
                    // System.out.println("Latitude:"+ ((JSONObject)
                    // jsonArray.get(i)).getString("GPSPoint_lat"));
                    // String id = ((JSONObject)
                    // jsonArray.get(i)).getString("deviceID");
                    // arr = (JSONArray) jsonArray.get(i);
                    JSONObject obj = secondarray.getJSONObject(i);
                    String lat = obj.getString("GPSPoint_lat");
                    String lon = obj.getString("GPSPoint_lon");

........//..
...///......}}

但是上面代码中的问题是,每当我执行时,我得到的 arraylength 是 1,即只得到一个值,我的意思是第一个 Eventdata 值。我需要的是我想获得所有的“Eventdata”值。请就此向我提出建议。

问候普里亚

4

7 回答 7

1
JSONObject object = jsonArray.getJSONObject(0);

我猜在上面的代码中,您只是在第一个索引处获取数据,该索引由“0”指定。因此,您也得到长度。尝试使用索引在循环内执行object.getJSONArray() 。也许这将帮助您获取“EventData”中的所有数据。希望这可以帮助 :)

于 2013-10-25T06:35:57.433 回答
0

JSON 中的问题是,在 eventData 数组中,您有 1 个 JSON 对象,其中包含很多映射,而不是 jsonObjects。如果您希望 EventData 成为一个 JSONArray

.."Geozone": "zone4",
                   "Geozone_index": 0,
                   "Address": "cxcxc",
                   "DigitalInputMask": 251,
                   "DigitalInputMask_hex": "0xFB",
                   "Index": 0...

作为 JsonObject,您需要将 JSON 更改为数组

..{"Geozone": "zone4"},
                   {"Geozone_index": 0},
                   {"Address": "cxcxc"},
                   {"DigitalInputMask": 251},
                   {"DigitalInputMask_hex": "0xFB"},
                  { "Index": 0}
...
于 2013-10-25T06:40:19.040 回答
0

Priya,您的 json 包含三个对象和两个数组,如下所示

对象 1. 整个响应 2. DeviceList 3. EventData

数组

  1. 设备列表 - 保存设备数组
  2. 事件数据 - 事件数组

现在仔细查看您的 JSON,该 json 包含 N 个设备,但每个设备都有一个事件,因此您必须迭代 DeviceList,然后迭代事件列表。

所以迭代应该如下。

JSONObject jsonObject = ApplicationContext.getHttpService().readAsJson(s);

JSONArray jsonDeviceListArray = jsonObject.getJSONArray("DeviceList");
int deviceCount = jsonDeviceListArray.length();

JSONObject jsonDeviceObj;
JSONArray jsonEventList;
for (int deviceIndex = 0; deviceIndex < deviceCount; deviceIndex++) {
    jsonDeviceObj = jsonDeviceListArray.getJSONObject(deviceIndex);
    //Here you can print device, device_desc from jsonDeviceObj

    jsonEventList = jsonDeviceObj.getJSONArray("EventData");
    int eventCount = jsonEventList.length();

    JSONObject jsonEventObj;
    for (int eventIndex = 0; eventIndex < eventCount; eventIndex++) {
        jsonEventObj = jsonEventList.getJSONObject(eventIndex);
        //Here you can print event info.
    }
}
于 2013-10-25T06:53:49.753 回答
0
 JSONArray DeviceListArray = jsonObject.getJSONArray("DeviceList");


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

            JSONObject Deviceobject = DeviceListArray.getJSONObject(i);


            JSONArray EventDataArray = Deviceobject.getJSONArray("EventData");

for (int j= 0; j < EventDataArray.length(); j++) {


                    JSONObject valueObj = EventDataArray.getJSONObject(j);
                    String GPSPoint_lat = valueObj.getString("GPSPoint_lat");
                    String GPSPoint_lon = valueObj.getString("GPSPoint_lon");
                    String Device = valueObj.getString("Device");
     // Store this three values in ArrayList
}
}

}
于 2013-10-25T06:44:23.243 回答
0

尝试这个..

JSONArray jsonArray = jsonObject.getJSONArray("DeviceList");

for (int j = 0; j < jsonArray.length(); j++) {

            JSONObject object = jsonArray.getJSONObject(j);

            JSONArray secondarray = object.getJSONArray("EventData");

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

                    System.out.println("Im...");
                    // System.out.println("Latitude:"+ ((JSONObject)
                    // jsonArray.get(i)).getString("GPSPoint_lat"));
                    // String id = ((JSONObject)
                    // jsonArray.get(i)).getString("deviceID");
                    // arr = (JSONArray) jsonArray.get(i);
                    JSONObject obj = secondarray.getJSONObject(i);
                    String lat = obj.getString("GPSPoint_lat");
                    String lon = obj.getString("GPSPoint_lon");


}
}
于 2013-10-25T06:35:17.770 回答
0

对于 JSON 解析,请使用以下类。

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;

public class JSONParser {

    public JSONParser() {

    }

    JSONObject jObj;
    String json;
    InputStream is = null;

    public JSONObject getJsonFromUrl(String url) {
        // TODO Auto-generated method stub

        try {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();

            is = httpEntity.getContent();
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;

            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();

            System.out.println("Json String : " + json);
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            jObj = new JSONObject(json);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jObj;
    }
}

并在您的 MainActivity 中使用以下代码对其进行解析。

JSONParser parser = new JSONParser();
JSONObject o = parser.getJsonFromUrl("yourjsonurl");

String Account = o.getString("Account");
String Account_desc = o.getString("Account_desc");

JSONArray array = o.getJSONArray("DeviceList");
JSONObject o1 = array.getJSONObject(0);
String Device = o1.getString("Device");

//Again you have JSONArray
JSONArray a = o1.getJSONArray("EventData");

//Then get object form array at index 0.

JSONObject obj = a.getJSONObject(0);

然后从这个 json 对象中获取你的数据。

于 2013-10-25T06:40:01.683 回答
-1

您的 json 数据中缺少“”。所有数字都应该有“”,因为在解析之前它被视为字符串。

于 2013-10-25T06:29:50.053 回答