0

I am new to parsing JSON in java. I have this JSON string:

[
{
 "projectId":5,
 "userName":"clinician",
 "projectName":"r",
 "projectSummary":"r",
 "projectLanguage":"r",
 "contactPersonName":"r",
 "contactPersonCV":"r",
 "contactPersonEmail":"r",
 "contactPersonPhone":"r"
},
[
 {
 "consentFileId":2,
 "projectId":5,
 "consentDescription":"r",
 "consentFileName":"test.pdf",
 "servicePathToGetConsentPdf":null
 },
 {
 "consentFileId":3,
 "projectId":5,
 "consentDescription":"rrr",
 "consentFileName":"test.pdf",
 "servicePathToGetConsentPdf":"localhost:8080/4c_viewFile?consentFileId=3"
 }
],
[
 {
 "anonymized_patient_identifier":"r",
 "projectId":5
 },
 {
 "anonymized_patient_identifier":"2",
 "projectId":5
 },
 {
 "anonymized_patient_identifier":"5",
 "projectId":5
 }
]

]

I have managed to get values from simpler JSON strings but this one has multiple levels and also there is no key in each level. I tried with simple code like this:

 Object obj = parser.parse(data);
 JSONObject jsonObject = (JSONObject) obj;
 resultJson = (String) jsonObject.get("projectId");
 resultJson += "\n";
 resultJson += (String) jsonObject.get("userName");

but I get the error [java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.simple.JSONObject] And also I don't understand how I will get the values in lower level without a key. I tried also to save it as a JSONArray but it didn't work.

asked May 12, 2015 at 13:07
2
  • 4
    The outermost element in your JSON is an array, and you're (inexplicably) trying to cast it to an object. Of course it doesn't work. Commented May 12, 2015 at 13:10
  • You should parse it to JSONArray, and then cast it to JSONObject with id. Commented May 12, 2015 at 13:13

2 Answers 2

4

your root of json is type of JSONArray, the first object stored in the root array is an object, you can retrieve it by using index = 0 .

this is a hack to make your code work:

 JSONArray jsonArray = JSONArray.fromObject(data);
 JSONObject jsonObject=obj.getJSONObject(0);
 resultJson = (String) jsonObject.get("projectId");
 resultJson += "\n";
 resultJson += (String) jsonObject.get("userName");

NOTE:

to convert a String to JSONArray, you can do :

JSONArray array = JSONArray.fromObject(data);
answered May 12, 2015 at 13:12
Sign up to request clarification or add additional context in comments.

11 Comments

I have another error now. I use netbeans and the error is: cannot find symbol about method getJSONObject(int). I have imported org.json.simple.JSONObject; and I searched it on goole but I only found articles about android.
@user1431148 have you imported this : net.sf.json.JSONArray; ? if yes make sure you've downloaded the newest package
I still have the same error. Also I use simple.JSONObject and if I use net.sf.json.JSONArray I get java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to net.sf.json.JSONArray. So which libraries should I use?
you should only use ONE json library. if you haven't done too much with org.json.simple package, then i recommend you replace all your code and start using net.sf.json.JSONArray.
Does it have a parser? Because I can't find it or I have to do it differently than JSONParser parser = new JSONParser(); Object obj = parser.parse(data);
|
1

To improve on nafas answer, I would do this to see all the objects in the array:

Object obj = parser.parse(data);
JSONArray jsonArray = (JSONArray) obj;
for (int i = 0; i < jsonArray.size (); i++) {
 JSONObject jsonObject=obj.getJSONObject(i);
 resultJson = (String) jsonObject.get("projectId");
 resultJson += "\n";
 resultJson += (String) jsonObject.get("userName");
}
answered May 12, 2015 at 13:16

1 Comment

I don't think it will work, the content of the array is heterogeneous and content are not all objects with that fields...

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.