0

At the moment i'm trying to understand json and how it works. But i have a problem with an array of objects. all objects in the array have a key called "value" (i know it's weird, it's not my code) what also is an object. And now to the problem: This object called "value" has always different key-values. So i dont now how i can parse the json code to java object code, when it differ, every time.

Here some examples:

First object of the array:

 "value":
 {
   "local":
   [
     "English", "Deutsch", Espanol"
   ],
  "english":
   [ 
     "English", "Deutsch", Espanol"
   ],
 },

Second object(now a string, not object) of the array:

"value" : "",

Third object of the array:

"value" : {},

...

Maybe I'm doing the parsing wrong. First I have created the beans classes in java for the json code and then I'm using the automatic parser of google. (gson) It works when only one of the examples above is inside the json code. (it should not differ, like changing from string to object...)

Gson gson = new Gson();
Output output = gson.fromJson(json, Output.class);

Output is the main class for the json stuff.

I have found out that maybe while parsing I could check a value called "id" first, and from that I could create another beans class with the right variables ...

Thats the code i need to parse to java objects and how do you do that?? The problem is the key called "value", because its always different. With my method of using the google parser "gson" it wont work, because i'm getting exception that its an string but i was waiting for an object...

{
 "status":"success",
 "data":{
 "panel":{
 "title":{
 "label":{ "local":"Tote Selection", "english":"Tote Selection" },
 "image":"public/img/pick.jpg", "type":"default"
 },
 "isFirst":false, // currently not used
 "isLast":false, // currently not used
 "ownCount":0, // currently not used
 "panelsCount":0, // currently not used
 "elements":[
 {
 "type":"text",
 "id":"1", "value":{ "local":"Scan next order tote", 
 "english":"Scan next order tote" },
 "label":{ "local":"", "english":"" }, "color":"000000", 
 "fontsize":18, "fontstyle":"flat", "alignment":"left",
 "rows":"undefined", "bgcolor":"", "isFocus":false
 },
 {
 "type":"text",
 "id":"4", "value":{ "local":"Scan tote: ", "english":"Scan tote: " },
 "label":{ "local":"", "english":"" }, "color":"000000", "fontsize":20, 
 "fontstyle":"strong", "alignment":"left", "rows":"undefined", 
 "bgcolor":"", "isFocus":false
 },
 {
 "type":"input",
 "id":"6", "value":"", "label":{ "local":"", "english":"" },
 "color":"000000", "fontsize":24, "fontstyle":"flat", "alignment":"left",
 "rows":"undefined", "isFocus":true
 },
 {
 "type":"button",
 "id":"1", "value":{ "local":"", "english":"" },
 "label":{ "local":"Menu", "english":"Menu" }, "color":"000000", 
 "fontsize":14, "fontstyle":"strong", "alignment":"left",
 "rows":"undefined", "isFocus":false
 },
 {
 "type":"button",
 "id":"4", "value":{ "local":"", "english":"" },
 "label":{ "local":"Enter", "english":"Enter" }, "color":"000000", 
 "fontsize":14, "fontstyle":"strong", "alignment":"right",18
 "rows":"undefined", "isFocus":false
 }
 ]
 },
 "authToken":"0fdd440a-619f-4936-ab74-d189accb5bd9",
 "routing":{
 "controller":"panel",
 "action":"process",
 "workflowId":"singlepicking",
 "taskId":"orderSelection"
 }
 }
 }

Thank you for your help!

4
  • so what is the requirement, is it to retrieve the values from JSON Array?? if yes just use a for loop. Commented Jul 15, 2014 at 5:25
  • yes all values of the array and his objects, strings .... Commented Jul 15, 2014 at 5:27
  • what do you mean with just use a for loop. I mean i cant do that if gson is parsing the code for me. Commented Jul 15, 2014 at 5:28
  • I have posted the code for simple json array dont know it will work for gson or not Commented Jul 15, 2014 at 5:32

2 Answers 2

1

it looks a little bit different but your answer helped me! Thx

        JsonParser parser = new JsonParser();
        JsonObject obj = parser.parse(br).getAsJsonObject();

        //now getting all the  json values
        String status = obj.get("status").getAsString();
        JsonObject data = obj.getAsJsonObject("data");

        String authToken = data.get("authToken").getAsString();
        JsonObject routing = data.getAsJsonObject("routing");
        String controller = routing.get("controller").getAsString();
        String action = routing.get("action").getAsString();
        String workflowId = routing.get("taskId").getAsString();
Sign up to request clarification or add additional context in comments.

Comments

0

If I understood ur question properly u can retrieve the values of the JSONArray as below

for (int i = 0; i < JArray.length(); i++) {
          print(JArray.getJSONObject(i).tostring())
        }

So if i am right u are getting the JSON from a String First?? so please try below first store the String in JSONObject as JSONObject obj = new JSONObject(str);//str is the string that u are getting

to get the valueenglish that are in data-panel-tittle-label is

String englishinLable=obj .getJSONObject("data").getJSONObject("panel").getJSONObject("title").getJSONObject("label").optString("english")

3 Comments

and the rest? its not only an array, look at the code i have posted now.
So if i am right u are getting the JSON from a String First??
ok i will try your code. It looks good, but i can only try it in 5 hours or so. If it then work i will accept your answer ;)

Your Answer

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.