2

Here is my structure of json data. It has huge amount of data similarly to this. I am using Jackson parser for parsing this.

{
 "dealers":
 {
  "google.com":{"id":1,"merchantname":"google","status":"active"},
  "apple.com":{"id":2,"merchantname":"apple","status":"active"}
 }
}

Code:

    while (jParser.nextToken() != JsonToken.END_OBJECT) {
            jParser.nextToken();

            while (jParser.nextToken() != JsonToken.END_OBJECT) {
                jParser.nextToken();

                while (jParser.nextToken() != JsonToken.END_OBJECT) {
                    jParser.nextToken();
                    String fieldname = jParser.getCurrentName();
                    if (fieldname != null) {

                        if ("id".equals(fieldname)) {
                            jParser.nextToken();
                            if (jParser.getText() != null)
                                merchantID = jParser.getText();
                            else
                                merchantID = "";
                        }

                        if ("merchantname".equals(fieldname)) {
                            jParser.nextToken();
                            if (jParser.getText() != null)
                                merchantname = jParser.getText();
                            else
                                merchantname = "";
                        }

                        if ("status".equals(fieldname)) {
                            jParser.nextToken();
                            if (jParser.getText() != null)
                                name = jParser.getText();
                        }

                    }

                }
            }
        }

The data is not parsed properly. Messed up with the jParser.nextToken() methods. Can anyone point out the mistake here?

2
  • There is no array in your json Commented Oct 7, 2013 at 18:51
  • Updated code. It goes infinite through the loop. Commented Oct 7, 2013 at 19:02

1 Answer 1

4

"dealers" property in JSON represents Map<String, POJO_CLASS>. You can easily convert it to below POJO classes:

class RootEntity {

    private Map<String, Entity> dealers;

    //getters,setters, toString
}

class Entity {

    private int id;
    private String merchantname;
    private String status;

    //getters,setters, toString
}

Example usage:

import java.io.File;
import java.io.IOException;
import java.util.Map;

import com.fasterxml.jackson.databind.ObjectMapper;

public class JacksonProgram {

    public static void main(String[] args) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        System.out.println(mapper.readValue(new File("/data/x.json"), RootEntity.class));
    }
}

prints:

RootEntity [dealers={google.com=Entity [id=1, merchantname=google, status=active], apple.com=Entity [id=2, merchantname=apple, status=active]}]
Sign up to request clarification or add additional context in comments.

5 Comments

I am getting com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "id" (class com.juteralabs.perkmobile.models.AllDealerssModel), not marked as ignorable (3 known properties: , "id", "merchantname", "status" [truncated]])
1. No, but your code is really ugly and really hard to maintenance. 2. This exception suggests that you want to deserialize JSON to wrong POJO or your JSON looks differently than in question.
It works. Thanks. But I am just curious to know what is wrong in the code I have posted? Unable to find out the mistake in that while loop.
How did you create "jParser" object? Debugger is your friend in this case.
jParser = jfactory.createParser(data); And data is a String

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.