0

I'm writing a program that receives a Json String, converts it to Java object and prints it out to the screen. Pretty simple stuff, but my problem is getting the program to read the Json String.

Below is the classes, along with the test class which creates a Json String, and calls the class NameList.java which creates a list of Name objects depending on the number of objects in the Json String.

Heres the error: Unrecognized field "Name" (Class sample.NameList), not marked as ignorable at [Source: java.io.StringReader@1a407d53; line: 1, column: 12] (through reference chain: sample.NameList["Name"])

I checked multiple sources online, many advised to just use annnotations in the list file which should fix it. Others say to change the name of the Xxx in' getXxx' to the name of the root in the JSON file. I tried both options, and none seemed to have made a difference. Any help would be much appreciated, Thanks

       public class UserTest {

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


        String jsonStr = "{"
                + "\"Names\":[{\"field\":\"John\","
                + "\"value\":\"3\"},{"
                + "\"field\":\"Ali\","
                + "\"value\":4 }]}";


        ObjectMapper mapper = new ObjectMapper();
        try {
            System.out.println("before obj creation");

            SelectList testObject = mapper.readValue(jsonStr, NameList.class); 
            System.out.println("Created the class");
            System.out.print(testObject);
        } catch (Exception e) {
            System.out.println("caught the error");
            e.printStackTrace();
        } 
    }

}



    public class Name {
        private String field;
        private String value;

        //getters and setters
@Override
    public String toString() {
        return "Name{" +
                "field='" + field + '\'' +
                ", value='" + value + '\'' +
                '}';
    }

       }



     public class SelectList {
    @JsonProperty("Select")
            private List<Name> name;

            /**
             * @return the selected statements
             */
            public List<Name> getName() {
                return name;
            }

            /**
             * @param the names to set
             */
            public void setName(List<Name> names) {
                this.name = names;
            }

    }
1

1 Answer 1

0

There are few issues in you code and json

     public class NameList {                                                                          
        @JsonProperty("Names")                   
        private List<Name> name;//field name should be same as json field or use annnotation                 

        /**                                      
         * @return the selected statements       
         */                                      
        public List<Name> getName() {            
            return name;                         
        }                                        

        /**                                      
         * @param  names to set                  
         */                                      
        public void setName(List<Name> names) {  
            this.name = names;                   
        }                                        

    }                                                                                   


    public class Name {
        private String field;
        private String value;

        public String getField() {
            return field;
        }

        public void setField(String field) {
            this.field = field;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }

        @Override
        public String toString() {
            return "Name{" +
                    "field='" + field + '\'' +
                    ", value='" + value + '\'' +
                    '}';
        }
    }


    public class NameListTest {

        @Test
        public void test1() { // values should be in quotes if they are string literal
            String jsonStr = "{"
                    + "\"Names\":[{\"field\":\"John\","
                    + "\"value\":\"3\"},{"
                    + "\"field\":\"Ali\","
                    + "\"value\":4 }]}";
            ObjectMapper mapper = new ObjectMapper();
            try {
                NameList testObject = mapper.readValue(jsonStr, NameList.class);
                System.out.print(testObject);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }                

Edited: Added maven dependencies

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.7.1</version>
    </dependency>
Sign up to request clarification or add additional context in comments.

9 Comments

I tried running it as such, with the annotation and got this error: org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "Name" (Class sample.NameList), not marked as ignorable
Jackson version 2.7.1
Shashank, I am not running this as a Maven project; That's what those dependencies are i would assume for the POM. Is there a way i can add that dependency into web.xml of the project? If so, how and where do I place the extensions. Thanks
1) Which version of jackson are you using.
I have: Jackson-xc-1.9.2, Jackson-mapper-asl.-1.9.2, Jackson-jaxrs-1.9.2, and finally Jackson-core-asl.1.9.2
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.