1

This is the JSON string:

{
   "d":{
      "results":[
         {
            "__metadata":{
               "uri":"http://blabla1",
               "type":"type1"
            },
            "Synonym":"miami"
         },
         {
            "__metadata":{
               "uri":"http://blabla2",
               "type":"type2"
            },
            "Synonym":"florida"
         }
      ]
   }
}

This is the code:

public class Test{        
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class d {
        public List<Results> results;
        public d() {
            results = new ArrayList<Results>();
        }
        public static class Results {
            public Metadata __metadata;
            public String Synonym;
            public Results() {}
        }
        public static class Metadata {
            public String uri;
            public String type;
            public Metadata() {}
        }
    }
}

With the following mapper:

ObjectMapper mapper = new ObjectMapper();
Test.d readValue = mapper.readValue(jsonString, Test.d.class);
for (Test.d.Results k : readValue.results) {
    System.out.println("synonym: "+k.Synonym);
}

It gives me no error, just an empty arraylist of results... p.s. i have made a lot of changes in between time, sorry for the inconvenience

1
  • 2
    Have you tried anything? Please include the code you have so far. Commented Nov 29, 2013 at 8:51

2 Answers 2

2

you must create an object that fits with the jSon answer, something like this (not tested):

class d {

    public List<Results> results;

    public d() {
    }
}

class Results {

    public Metadata metadata;
    public String synonym;

    public Results() {
    }
}

class Metadata {

    public String uri;
    public String type;


    public Metadata() {
    }
}

Hope it helps!

Sign up to request clarification or add additional context in comments.

3 Comments

What could be the problem? your code seems to be correct but it gives me no result
As I see in your editted answer, you don't respect the jSon heriachy of your response. The d object only has a Results List, and you have putted in the others classes. Try to do exactly as I made, using 3 different classes, no ones inside the others.
it says: "org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "d" (Class d), not marked as ignorable"- If I put "@JsonIgnoreProperties(ignoreUnknown = true)" it runs without error but when I want to read the contents of results, it crashes (nullpointer exception)
1

I have managed to solve it. i forgot to make setters and getters for class 'd'.

public class Test {
    private d d;
    public d getD() {return d;}
    public void setD(d d) {this.d = d;}    
    public static class d{
        private List<Results> results;
        public List<Results> getResults() {return results;}
        public void setResults(List<Results> results) {this.results = results;}
    }
    public static class Results {
        public Metadata __metadata;
        public String Synonym;
    }
    public static class Metadata {
        private String uri;
        private String type;
        public String getUri() {return uri;}
        public void setUri(String uri) {this.uri = uri;}
        public String getType() {return type;}
        public void setType(String type) {this.type = type;}
    }
}

This is the map:

Test test = mapper.readValue(json, KeyPhrase.class);
    System.out.println("cp");

    for(Test.Results res : test.getD().getResults()){
        System.out.println(res.Synonym);
}

2 Comments

Great! I hope that my answer helped you!
Yes it did, gave me a greater understanding of how mapping works! Thank you

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.