Amarsaikhan
Amarsaikhan

Reputation: 19

How to json parse this data?

How to json parse this data?
I did not parse it is.

{"boh" : {
      "-LBktL2BIRnG6yZPzX9M" : {
        "address" : "Улаанбаатар",
        "firstname" : "Болд",
        "floatt" : "9",
        "lastname" : "Бат ",
        "title" : "улсын начин"
      },
      "-LBktTUZMDLqkoMR49Gv" : {
        "address" : "Улаанбаатар",
        "firstname" : "Эрдэнэ",
        "floatt" : "",
        "lastname" : "Болд",
        "title" : "залуу бөх"
      },
      "-LBkt_OIvRGD-r_L40EH" : {
        "address" : "Улаанбаатар",
        "firstname" : "Нямхүү",
        "floatt" : "5",
        "lastname" : "Буянжаргал",
        "title" : "улсын заан"
      }
};

I did write below code:;

 JSONObject obj1 = new JSONObject(jsonString);
                        JSONArray result = obj1.getJSONArray("boh");
                            for(int i=0;i<=result.length();i++)
                            {
                                JSONObject c = result.getJSONObject(i);
                                String address=c.getString("address");
                                String firstname=c.getString("firstname");
                                String floatt=c.getString("floatt");
                                String lastname=c.getString("lastname");
                                String title=c.getString("title");

                            };

But, JSONException error

How to a solve parse in android help me?

Upvotes: 0

Views: 66

Answers (3)

MJM
MJM

Reputation: 5291

Using Iterator you can pass the data

 JSONObject obj1 = new JSONObject(jsonString);
    JSONObject result = obj1.getJSONObject("boh");
    Iterator<String> iter = result.keys();
    while (iter.hasNext()) {
        String key = iter.next();
        JSONObject c = result.getJSONObject(key);
        String address=c.getString("address");
        String firstname=c.getString("firstname");
        String floatt=c.getString("floatt");
        String lastname=c.getString("lastname");
        String title=c.getString("title");

    }            

Upvotes: 1

Michael Dodd
Michael Dodd

Reputation: 10270

boh is not a JSON array, it is a JSON Object with children. If boh was an array, it would look like this (note the square brackets [ ]):

{"boh" : [ 
  {
    "address" : "Улаанбаатар",
    "firstname" : "Болд",
    "floatt" : "9",
    "lastname" : "Бат ",
    "title" : "улсын начин"
  },
  {
    "address" : "Улаанбаатар",
    "firstname" : "Эрдэнэ",
    "floatt" : "",
    "lastname" : "Болд",
    "title" : "залуу бөх"
  },
  {
    "address" : "Улаанбаатар",
    "firstname" : "Нямхүү",
    "floatt" : "5",
    "lastname" : "Буянжаргал",
    "title" : "улсын заан"
  }]
}

So in order to access these child objects, you would need to treat boh as a JSONObject and access its children in order:

JSONObject obj1 = new JSONObject(jsonString);
JSONObject result = obj1.getJSONObject("boh");

Iterator<String> keys = result.keys();
while(keys.hasNext()) {
    String key = keys.next();
    JSONObject c = result.getJSONObject(key);

    String address=c.getString("address");
    String firstname=c.getString("firstname");
    String floatt=c.getString("floatt");
    String lastname=c.getString("lastname");
    String title=c.getString("title");
};

Upvotes: 0

Mayur Raval
Mayur Raval

Reputation: 3275

Use Gson library

Class for Inner map value

public class Example{
  @SerializedName("firstname")
  @Expose
  private String firstname;
  @SerializedName("address")
  @Expose
  private String address;
  @SerializedName("floatt")
  @Expose
  private Integer floatt;
  @SerializedName("title")
  @Expose
  private String title;
  @SerializedName("lastname")
  @Expose
  private String lastname;
  public void setFirstname(String firstname){
   this.firstname=firstname;
  }
  public String getFirstname(){
   return firstname;
  }
  public void setAddress(String address){
   this.address=address;
  }
  public String getAddress(){
   return address;
  }
  public void setFloatt(Integer floatt){
   this.floatt=floatt;
  }
  public Integer getFloatt(){
   return floatt;
  }
  public void setTitle(String title){
   this.title=title;
  }
  public String getTitle(){
   return title;
  }
  public void setLastname(String lastname){
   this.lastname=lastname;
  }
  public String getLastname(){
   return lastname;
  }
}

For parsing

Map<String, HashMap<String, Example>> categoryMap = new Gson().fromJson(br, new TypeToken<Map<String, HashMap<String, Example>>>(){}.getType());

For getting value

Iterator it = categoryMap.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry pair = (Map.Entry)it.next();
      Iterator it = ((HashMap<String, Example>>)categoryMap.get(pair.getKey())).entrySet().iterator();
       while (it.hasNext()) {
        System.out.println(pair.getKey() + " = " + pair.getValue());
         Example mExample = (Example)categoryMap.get(pair.getKey());//
      }
        it.remove(); // avoids a ConcurrentModificationException
    }

Upvotes: 0

Related Questions