Anupam
Anupam

Reputation: 3752

JSON Parsing Using Jackson Library

My JSON data looks like this:

[
{
  "id":1,
  "ad_name":"Test Ad",
  "ad_text":"This is my ad text"
},
{
  "id":2,
  "ad_name":"Test Ad",
  "ad_text":"This is my ad text"
},
{
  "id":3,
  "ad_name":"Test Ad",
  "ad_text":"This is my ad text"
},
{
  "id":4,
  "ad_name":"Test Ad",
  "ad_text":"This is my ad text"
},
{
  "id":5,
  "ad_name":"Test Ad",
  "ad_text":"This is my ad text"
}
]

I'm using Jackson Library to parse it, here is my code for that:

try {
        jParser = jfactory.createParser(array);

        while (jParser.nextToken() != JsonToken.END_ARRAY) {

            String fieldname = jParser.getCurrentName();

            if (fieldname != null) {

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

                    System.out.println(id);
                }

                if ("ad_name".equals(fieldname)) {
                    jParser.nextToken();
                    if (jParser.getText() != null)
                        ad_name = jParser.getText();

                    System.out.println(ad_name);
                }

                if ("ad_text".equals(fieldname)) {
                    jParser.nextToken();
                    if (jParser.getText() != null)
                        ad_text = jParser.getText();

                    System.out.println(ad_text);
                }
            }

        }

        jParser.close();
        return adsList;
    } catch (Exception e) {
        e.printStackTrace();
    }

My parsing and all stuff are working fine. But when I'm printing values in logs, I'm getting duplicate values, means every JSONObject is printing two times.

What am I missing here? Any kind of help will be appreciated.

Upvotes: 2

Views: 12117

Answers (3)

Anupam
Anupam

Reputation: 3752

I have solved it using the following parsing method, I have used the same Jackson library for parsing it:

try {

        jParser = jfactory.createParser(array);

        if (jParser.nextToken() == JsonToken.START_ARRAY) {

            while (jParser.nextToken() != JsonToken.END_ARRAY) {

                if (jParser.nextToken() == JsonToken.FIELD_NAME) {

                    String fieldname = jParser.getCurrentName();

                    if (fieldname != null) {

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

                    if (jParser.nextToken() == JsonToken.FIELD_NAME) {

                        String fieldname1 = jParser.getCurrentName();

                        if ("ad_name".equals(fieldname1)) {
                            jParser.nextToken();
                            if (jParser.getText() != null)
                                ad_name = jParser.getText();
                        }
                    }

                    if (jParser.nextToken() == JsonToken.FIELD_NAME) {

                        String fieldname2 = jParser.getCurrentName();

                        if ("ad_text".equals(fieldname2)) {
                            jParser.nextToken();
                            if (jParser.getText() != null)
                                ad_text = jParser.getText();
                        }
                    }

                    if (jParser.nextToken() == JsonToken.FIELD_NAME) {

                        String fieldname3 = jParser.getCurrentName();

                        if ("ad_image_url".equals(fieldname3)) {
                            jParser.nextToken();
                            if (jParser.getText() != null)
                                ad_image_url = jParser.getText();
                        }
                    }

                    if (jParser.nextToken() == JsonToken.FIELD_NAME) {

                        String fieldname4 = jParser.getCurrentName();

                        if ("ad_click_url".equals(fieldname4)) {
                            jParser.nextToken();
                            if (jParser.getText() != null)
                                ad_click_url = jParser.getText();

                        }

                        adsList.add(new AllAdsModel(id, ad_name, ad_text,
                                ad_image_url, ad_click_url));
                    }
                }
            }

        }

        jParser.close();
        return adsList;
    } catch (Exception e) {
        e.printStackTrace();
    }

Hope, it will help others!

Upvotes: 2

Michał Ziober
Michał Ziober

Reputation: 38645

Your JSON is really simple. You can easily deserialize it to Java POJO objects with this example. Create simple POJO for array item.

class Ad {

    private int id;
    @JsonProperty("ad_name")
    private String name;
    @JsonProperty("ad_text")
    private String text;

        //getters, setters, toString()
}

And now, you can deserialize it like this:

ObjectMapper mapper = new ObjectMapper();

Ad[] array = mapper.readValue(json, Ad[].class);
System.out.println(Arrays.toString(array));

Above program prints:

[Ad [id=1, name=Test Ad, text=This is my ad text], Ad [id=2, name=Test Ad, text=This is my ad text], Ad [id=3, name=Test Ad, text=This is my ad text], Ad [id=4, name=Test Ad, text=This is my ad text], Ad [id=5, name=Test Ad, text=This is my ad text]]

I would like to help you with your source code, but it is not complete. For me your source code prints each property only once. Maybe you have a bug in the code which we are not able to see?

Upvotes: 4

kjurkovic
kjurkovic

Reputation: 4104

I do not know actually what is wrong with your code, but if you parse JSON the way you do, you actually don't need Jackson. You might as well use JSONObject.

You can do it more efficiently this way with Jackson:

Ad.java

public class Ad {
 public int id;
 public String ad_name;
 public String ad_text;
}

Parser

JsonFactory jsonFactory = new JsonFactory();
JsonParser jsonParser = f.createJsonParser(your_json_string);
jsonParser.nextToken();
List<Ad> adList = new ArrayList<Ad>();
while (jp.nextToken() == JsonToken.START_OBJECT)) {
   Ad ad = mapper.readValue(jsonParser, Ad.class);
   adList.add(ad);
}
  • smart thing to have are getters and setters in Ad.java as well.

Upvotes: 3

Related Questions