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;
            }

    }
Almanz
  • 79
  • 1
  • 10

1 Answers1

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>
Shashank
  • 416
  • 5
  • 16
  • 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 – Almanz Nov 05 '16 at 16:20
  • Jackson version 2.7.1 – Shashank Nov 05 '16 at 16:21
  • 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 – Almanz Nov 05 '16 at 16:57
  • 1) Which version of jackson are you using. – Shashank Nov 05 '16 at 17:11
  • 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 – Almanz Nov 05 '16 at 17:15
  • I'm using eclipse, with an apache tomcat version6. JDK 1.8.0_101 – Almanz Nov 05 '16 at 17:16
  • I may be missing something; You mentioned what tool to manage dependencies, what does it mean to manage dependencies? Sorry if this is a rookie question, as i have not followed a route of managing dependencies – Almanz Nov 05 '16 at 17:24
  • Something like maven /ivy/gradle etc. It is tool which manages all your dependencies. For example jackson uses let says common lib if you do this manually by adding in class path you have download and add jackson and commonlib. But if you use dependency manager, it will automatically manage all this. When project grows bigger it is reallly hard keep track of dependecies; it helps a lots. Also now you donot need know all dependencies used by libs you are using in your project – Shashank Nov 05 '16 at 17:33
  • Thanks so much, you where a great help – Almanz Nov 05 '16 at 19:26