【问题标题】:I'm having trouble accessing elements in a JSON object in JAVA我在访问 JAVA 中的 JSON 对象中的元素时遇到问题
【发布时间】:2016-03-30 12:48:29
【问题描述】:

我正在从 Mongodb 数据库中提取数据。我需要访问数据中的特定项目,但我没有运气。我可以打印整个对象并查看它的结构,但是当尝试访问 JSON 对象中的对象时,我得到了一个异常; 抛出异常 org.json.JSONException: JSONObject["forms"] not found。我一直无法弄清楚这个问题,任何帮助将不胜感激。这是 JSON 对象和我用来解析它的代码:

{"_id":{"$oid":"56ec24fce4b0322e5e267fc4"}
, "dateModified":{"$numberLong":"1458316466081"}
, "id":"56ec24aae4b0322e5e267e41"
, "projectLabel":"CLT119-050-002"
, "projectId":"CLT119-050-002"
, "clientFile":"DEC_5.3.client"
, "clientFileVersion":"9ce6fad040358033c98db66c7ea80fe8"
, "calcLocation":{"label":"19NC59"
, "mapNumber":""
, "comments":""
, "address":{"number":"2120"
, "street":"N DAVIDSON ST"
, "city":"CHARLOTTE"
, "county":"MECKLENBURG"
, "state":"NC"
, "zip_code":"28206"}
, "technician":"F PIERCE"
, "crossStreet1":""
, "crossStreet2":""
, "geographicCoordinate":{"type":"Point"
, "coordinates":[-80.81569166666667
, 35.23912333333333
, 238.3]}
, "remedies":[{"description":"TWC: At 20' 6\" lower 0' 6\" to 20' 0\""}
, {"description":"AT&T: At 21' 1\" lower 1' 1\" to 19' 0\" span NorthWest"}
, {"description":"GFI: Attach at 18' 0\""}]
, "poleTags":[]
, "userDefinedValues":{}
, "summaryNotes":["At Pole Clearance Violation"]
, "forms":[ 
, {"title":"Billing Code (Client)"
, "template":"9ce6fad040358033c98db66c7ea80fe8-Billing Code (Client)"
, "fields":{"Billing Code":"Remedy Pole"
, "Data Collection":"Select One"
, "2nd Trip Fee":"false"
, "ReEngineered":"false"
, "2nd ReEngineered":"false"
, "3rd ReEngineered":"false"
, "Comments":""}}]
, "images":[{"url":"19NC59-1.jpg"
, "link":{"id":"f931f69c-0748-433e-b186-31cc04abd9a1"
, "source":"filefortAssetService"}}
, {"url":"19NC59-2.jpg"
, "link":{"id":"e3517acf-ff79-4ad8-ba33-fac6e75f0c80"
, "source":"filefortAssetService"}}
, {"url":"19NC59-3.jpg"
, "link":{"id":"ecf92caf-c5ff-4d68-b84a-14805186bab9"
, "source":"filefortAssetService"}}]
, "designs":[{"id":"56ec24aae4b0322e5e267e3e"
, "label":"Existing"}
, {"id":"56ec24aae4b0322e5e267e3f"
, "label":"Proposed"}
, {"id":"56ec24aae4b0322e5e267e40"
, "label":"Remedy"}]
, "id":"56ec24aae4b0322e5e267e41"}
, "user":{"id":"0"
, "email":"admin@someCompany.com"}}

代码:

FindIterable<Document> iterable = db.getCollection("locations").find(new Document("projectLabel", "CLT119-050-002"));
    iterable.forEach(new Block<Document>() {
        @Override
        public void apply(final Document document) {
            try{
                JSONObject json = new JSONObject(document.toJson());
                JSONArray forms = json.getJSONArray("forms");       
            }catch(Exception e){
                System.out.println(e);
            }
        }

对象 json 中包含数据,尝试填充 JSONArray 会引发异常。

【问题讨论】:

  • 您的 json 似乎无效。你可以在这里查看:jsonlint.com

标签: java json mongodb


【解决方案1】:

问题是您尝试访问的对象是 calcLocation.forms 而不是简单的表单。

【讨论】:

  • 你是对的,我完全错过了嵌套。谢谢迈克
猜你喜欢
  • 2019-08-25
  • 2011-06-09
  • 1970-01-01
  • 2020-07-03
  • 2021-09-27
  • 2021-03-31
  • 1970-01-01
  • 2021-05-12
  • 2017-10-12
相关资源
最近更新 更多