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

I';我在JAVA中访问JSON对象中的元素时遇到问题,java,json,mongodb,Java,Json,Mongodb,我正在从Mongodb数据库中提取数据。我需要访问数据中的特定项目,但我运气不好。我可以打印整个对象并查看它的结构,但是当尝试访问JSON对象中的对象时,我得到一个异常; 异常抛出org.json.JSONException:JSONObject[“forms”]未找到。我一直无法解决这个问题,任何帮助都将不胜感激。下面是JSON对象和我用来解析它的代码: {"_id":{"$oid":"56ec24fce4b0322e5e267fc4"} , "dateModified":{"$numberL

我正在从Mongodb数据库中提取数据。我需要访问数据中的特定项目,但我运气不好。我可以打印整个对象并查看它的结构,但是当尝试访问JSON对象中的对象时,我得到一个异常; 异常抛出org.json.JSONException:JSONObject[“forms”]未找到。我一直无法解决这个问题,任何帮助都将不胜感激。下面是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);
            }
        }
FindIterable-iterable=db.getCollection(“位置”).find(新文档(“项目标签”、“CLT119-050-002”);
iterable.forEach(新块(){
@凌驾
公开作废申请(最终文件){
试一试{
JSONObject json=新的JSONObject(document.toJson());
JSONArray forms=json.getJSONArray(“forms”);
}捕获(例外e){
系统输出打印ln(e);
}
}

对象json中有数据,试图填充JSONArray时抛出异常。

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

您的json似乎无效。您可以在此处检查:您是正确的,我完全错过了嵌套。谢谢Mike