简体   繁体   English

我在访问JAVA中的JSON对象中的元素时遇到问题

[英]I'm having trouble accessing elements in a JSON object in JAVA

I'm pulling data out of a Mongodb database. 我正在从Mongodb数据库中提取数据。 I need to access specific items in the data but I'm having no luck. 我需要访问数据中的特定项目,但是我没有运气。 I can print the the entire object and see it's structure but when trying access objects inside the JSON object I get an exception; 我可以打印整个对象并查看其结构,但是当尝试访问JSON对象内部的对象时,出现异常; exception thrown org.json.JSONException: JSONObject["forms"] not found. 抛出org.json.JSONException异常:找不到JSONObject [“ forms”]。 I have been unable to figure out the issue, any help would be greatly appreciated. 我一直无法弄清问题,我们将不胜感激。 Here is the JSON object and the code I'm using to parse it: 这是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"}}

the code: 编码:

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);
            }
        }

the object json has the data in it trying to populate the JSONArray throws the exception. 对象json中有试图填充JSONArray的数据将引发异常。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM