android - Parsing JSON Object inside Array in Jackson -
am using jackson json processor details form json, have following
json string
{ "first_name": "first_name", "last_name": "last", "city": "somewhere", "user_user": [ { "id": "1", "domain": "http://google.com/" }, { "id": "34", "domain": "http://so.com/" }, { "id": "42", "domain": "http://ww.com/" } ]}
userdetails
package com.example.com; import java.util.list; public class userdetails{ private string city; private string first_name; private string last_name; private list<?> user_user; public string getcity(){ return this.city; } public void setcity(string city){ this.city = city; } public string getfirst_name(){ return this.first_name; } public void setfirst_name(string first_name){ this.first_name = first_name; } public string getlast_name(){ return this.last_name; } public void setlast_name(string last_name){ this.last_name = last_name; } public list<?> getuser_user(){ return this.user_user; } public void setuser_user(list<?> user_user){ this.user_user = user_user; } }
user_user
package com.example.com; import java.util.list; public class user_user{ private string domain; private string id; public string getdomain(){ return this.domain; } public void setdomain(string domain){ this.domain = domain; } public string getid(){ return this.id; } public void setid(string id){ this.id = id; } }
code
objectmapper mapper = new objectmapper(); userdetails userdetails = mapper.readvalue(jsonstring , userdetails.class); system.out.println(userdetails.getfirst_name()); system.out.println(userdetails.getlast_name()); user_user userf = mapper.readvalue(jsonstring , user_user.class); for(int = 0; < userdetails.getuser_user().size(); i++) { system.out.println(userf.getid()); }
from above code not able id
user_user
.how parse , get
fav_colors
if json had array again.
i changed list<?> user_user;
list<user_user> user_user; in userdetails
suggested michaĆ ziober
after able retrieve results easily, here code.
without sysout it's hardly 3 lines
.
system.out.println(userdetails.getcity()); //city, firstname, lastname for(int = 0; < userdetails.getuser_user().size(); i++) { system.out.println(userdetails.getuser_user().get(i).getid()); // id, domain try { for(int k = 0; k < userdetails.getuser_user().get(i).getfav_colors().size(); k++) { system.out.println(userdetails.getuser_user().get(i).getfav_colors().get(k)); //fav_color } } catch (exception e) { system.out.println("no fav colors"); } }
for reference have added project in github.
Comments
Post a Comment