1

I get some JSON code like this:

{"1":{"id":"1","Vorname":"x","Nachname":"y","MaleFemale":0,"interests":[]},
 "2":{"id":"2","Vorname":"x","Nachname":"y","MaleFemale":1,"interests":[]},
 ...

from my PHP script. Could you tell me how to decode this format in Java? I only get examples where you have to have to have a format like this:

{"contacts": [{"user.id":"1","Vorname":"x","Nachname":"y","MaleFemale":1},
              {"user.id":"2","Vorname":"x1","Nachname":"y2","MaleFemale":0}]}

So the difference is that in the first given code there is no "main node". In the second given code there is one ("contacts"). Do I need this node? I try so much but i do not get how to work this out.

Thank you very much.

Benjamin Wolf
  • 93
  • 1
  • 9

2 Answers2

0

I thinks you should use jackson mapper. here is a link: How to convert Java object to / from JSON (Jackson)

hamed
  • 7,939
  • 15
  • 60
  • 114
0

You can do this easily with Jackson java library. Here is an example code snippet.

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import java.io.IOException;

public class Test {

    public static void main(String[] args) throws IOException {

        ObjectMapper mapper = new ObjectMapper();
        // Reading the string to a JSON object
        JsonNode jsonObject = mapper.readTree("{\"contacts\": [{\"user.id\":\"1\",\"Vorname\":\"x\",\"Nachname\":\"y\",\"MaleFemale\":1},\n" +
                "              {\"user.id\":\"2\",\"Vorname\":\"x1\",\"Nachname\":\"y2\",\"MaleFemale\":0}]}");

        //Some basic querying
        JsonNode contacts = jsonObject.get("contacts");
        if (contacts.isArray()){
            ArrayNode contactsArray = (ArrayNode) contacts;
            for (JsonNode contact : contactsArray) {
                System.out.println(contact.get("user.id"));
            }
        }

    }
}

You can download the Jackson library from here.

Nandana
  • 1,240
  • 8
  • 17
  • Hi thank you for your post, but i do absolutly not get how to download the library from this site. Could you help me with this too please? – Benjamin Wolf Jan 24 '15 at 22:47
  • Are you using maven or some other build system? – Nandana Jan 24 '15 at 22:57
  • Sorry I finally was able to find it. I try your code and post my result. – Benjamin Wolf Jan 24 '15 at 22:57
  • It throws this error in Android Studio when i want to compile: Error:(491, 36) error: cannot access ObjectCodec class file for com.fasterxml.jackson.core.ObjectCodec not found – Benjamin Wolf Jan 24 '15 at 23:05
  • Which libraries did you add to your class path? I think the first one should be enough, and it the one that contains ObjectCodec class. But may be you will have to add the other two as well. http://repo1.maven.org/maven2/com/fasterxml/jackson/core/jackson-core/2.5.0/jackson-core-2.5.0.jar http://repo1.maven.org/maven2/com/fasterxml/jackson/core/jackson-annotations/2.5.0/jackson-annotations-2.5.0.jar http://repo1.maven.org/maven2/com/fasterxml/jackson/core/jackson-databind/2.5.0/jackson-databind-2.5.0.jar – Nandana Jan 24 '15 at 23:11
  • Hi, if I add the given librarys there are this errors on compile: Error:Execution failed for task ':app:packageDebug'. > Duplicate files copied in APK META-INF/LICENSE File 1: path\app\libs\jackson-core-2.5.0.jar File 2: path\app\libs\jackson-core-2.5.0.jar – Benjamin Wolf Jan 24 '15 at 23:39