java - How to handle json array parsing with Jackson parser in Android -


here structure of json data. has huge amount of data this. using jackson parser 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 not parsed properly. messed jparser.nexttoken() methods. can point out mistake here?

"dealers" property in json represents map<string, pojo_class>. can convert 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]}] 

Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

c++ - Correct method for redrawing a layered window -

java.util.scanner - How to read and add only numbers to array from a text file -