0

所以,这是我拥有的 json 字符串。

String json ="{\"BPDataList\":[{\"BPL\":5,\"DataID\":\"6deacbc373e74e1794a7*****\",\"HP\":124,\"HR\":82,\"IsArr\":-1,\"LP\":82,\"Lat\":-1,\"Lon\":-1,\"MDate\":1373392309,\"Note\":\"\",\"LastChangeTime\":1373362486,\"DataSource\":\"FromDevice\",\"TimeZone\":\"+0800\"}],\"BPUnit\":0,\"CurrentRecordCount\":50,\"NextPageUrl\":\"https%3a%2f%2fapi.ihealthlabs.com%3a8443%2fopenapiv2%2fuser%2f05dffbe0dd*****%2fbp.json%2f%3fclient_id%3dddb9cbc759*****%26client_secret%3d4738f9d00e*****%26redirect_uri%3dhttp%253a%252f%252fapi.testweb2.com%252foauthtest.aspx%26access_token%3dxpoBt0ThQQ*****%26start_time%3d1342007016%26end_time%3d1405079016%26page_index%3d2%26sc%3dd63493704c*****%26sv%3d113cb40956*****\",\"PageLength\":50,\"PageNumber\":1,\"PrevPageUrl\":\"\",\"RecordCount\":335}";

我正在使用此代码将其存储到地图中;

Map<String, Object> map = new HashMap<String, Object>();
JSONObject jObject = new JSONObject(json);

Iterator<?> keys = jObject.keys();
while (keys.hasNext()) {
    try {
        String key = (String) keys.next();
        Object value = jObject.getJSONArray(key);
        log.debug("value--" + map);
        map.put(key, value);
    } catch (Exception e) {
        log.trace("getUserBG--", e);
    }
}

但是,它只将第一个元素存储到地图中,即 BPDataList。我在这里做错了什么?

这是地图的内容,当我打印它时。

map:{BPDataList=[{"DataSource":"FromDevice","IsArr":-1,"DataID":"6deacbc373e74e1794a7*****","BPL":5,"HR":82,"MDate":1373392309,"Lat":-1,"Note":"","HP":124,"TimeZone":"+0800","Lon":-1,"LastChangeTime":1373362486,"LP":82}]}

json

{
  "BPDataList": [
    {
      "BPL": 5,
      "DataID": "6deacbc373e74e1794a7*****",
      "HP": 124,
      "HR": 82,
      "IsArr": -1,
      "LP": 82,
      "Lat": -1,
      "Lon": -1,
      "MDate": 1373392309,
      "Note": "",
      "LastChangeTime": 1373362486,
      "DataSource": "FromDevice",
      "TimeZone": "+0800"
    }
  ],
  "BPUnit": 0,
  "CurrentRecordCount": 50,
  "NextPageUrl": "abc",
  "PageLength": 50,
  "PageNumber": 1,
  "PrevPageUrl": "",
  "RecordCount": 335
}
4

1 回答 1

1

这是因为除了 BPDataList 之外的所有内容都不是 JSONArray。

将您的代码更改为

while (keys.hasNext()) {
    try {
        String key = (String) keys.next();
        Object value = jObject.get(key);
        log.debug("value--" + map);
        map.put(key, value);
    } catch (Exception e) {
        log.trace("getUserBG--", e);
    }
}

这应该可以正常工作

于 2015-08-16T12:31:42.080 回答