0

I have an array "myarray" like this:

[{
   name: John,
   age: {
     years:18
   },
   computer_skills: {
     years:4
   },
   mile_runner: {
     years:2
   }
}]

How do I uplevel it properly? Using minimal-json, I do the following:

JsonArray jsonArray = JsonArray.readFrom(myarray);
for( JsonValue val : jsonArray) {
    JsonObject myObj = val.asObject();
    for( JsonObject.Member myMember : myObj ) {
        if(myMember.getValue().isObject()) {
            JsonObject myMemberObj = myMember.getValue().asObject();
            for (JsonObject.Member nestedMember : myMemberObj) {
                if(nestedMember.getName().equals("years")) {
                    myObj.set(myMember.getName(), nestedMember.getValue());
                    break;
                }
            }
        }
    }
}
System.out.println(jsonArray);

The last line when I print out my jsonArray, it looks as if nothing has changed whatsoever. What am I doing wrong? I want to end up with the following:

[{
   name: John,
   age: 18
   computer_skills: 4
   mile_runner: 2
}]

I tried using this: https://github.com/ralfstx/minimal-json. Alternatives are welcome. I want to not have to create a model object that contains key value pairs for every single key value in my object within "myarray". I am used to python where everything is simple to access and replace.

Arthur Eirich
  • 3,368
  • 9
  • 31
  • 63
Rolando
  • 58,640
  • 98
  • 266
  • 407
  • why not using jackson? – Arthur Eirich Jun 15 '15 at 05:24
  • Not sure what you mean by jackson, but I dont want to define a "Model" object that maps to all my values. I am used to python where I can just access things directly. Not sure if jackson would be simpler for what I am trying to do, I am open to anything. I heard org.json was "slower" than some alternatives, not sure if true or not. – Rolando Jun 15 '15 at 05:25
  • Gson from Google seems to be not the worst one, I myself used JsonLib in my university project, there is also json-simple – Arthur Eirich Jun 15 '15 at 05:27

1 Answers1

0

After some researching I found out that your json array is invalid, there are quotes missing on at least the keys (See why do you need the quotes). Look at the same code you used but with modified input for 'myarray':

String myarray = "[{\"name\":\"John\",\"age\":{\"years\":18},\"computer_skills\":{\"years\":4},\"mile_runner\":{\"years\":2}}]";
    JsonArray jsonArray = JsonArray.readFrom(myarray);
    for( JsonValue val : jsonArray) {
        JsonObject myObj = val.asObject();
        for( JsonObject.Member myMember : myObj ) {
            if(myMember.getValue().isObject()) {
                JsonObject myMemberObj = myMember.getValue().asObject();
                for (JsonObject.Member nestedMember : myMemberObj) {
                    if(nestedMember.getName().equals("years")) {
                        myObj.set(myMember.getName(), nestedMember.getValue());
                        break;
                    }
                }
            }
        }
    }
    System.out.println(jsonArray);

This code prints:

[{"name":"John","age":18,"computer_skills":4,"mile_runner":2}]

what is the expected result.

Community
  • 1
  • 1
Arthur Eirich
  • 3,368
  • 9
  • 31
  • 63