我从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);
}
}
对象json中有数据试图填充JSONArray抛出异常。
答案 0 :(得分:0)
问题是您尝试访问的对象是 calcLocation.forms 而不是简单的表单。