问题描述
我使用的杰克逊JSON处理器,以获得详细信息形成的JSON,我有以下
JSON字符串
{
FIRST_NAME:FIRST_NAME
姓氏:最后一个,
城市:在某处,
user_user:[
{
ID:1
域:http://google.com/
},
{
ID:34,
域:http://so.com/
},
{
ID:42,
域:http://ww.com/
}
]}
的UserDetails
包com.example.com;进口的java.util.List;公共类的UserDetails {
私人字符串的城市;
私人字符串FIRST_NAME;
私人字符串姓氏;
私人列表<> user_user;公共字符串getCity(){
返回this.city;
}
公共无效setCity(字符串市){
this.city =城市;
}
公共字符串getFirst_name(){
返回this.first_name;
}
公共无效setFirst_name(字符串FIRST_NAME){
this.first_name = FIRST_NAME;
}
公共字符串getLast_name(){
返回this.last_name;
}
公共无效setLast_name(字符串姓氏){
this.last_name =姓氏;
}
公开名单<> getUser_user(){
返回this.user_user;
}
公共无效setUser_user(列表<> user_user){
this.user_user = user_user;
}
}
User_user
包com.example.com;进口的java.util.List;公共类User_user {
私人字符串域;
私人字符串ID;公共字符串getDomain(){
返回this.domain;
}
公共无效setDomain(字符串域){
this.domain =域;
}
公共字符串的getId(){
返回this.id;
}
公共无效SETID(字符串ID){
this.id = ID;
}
}
code
ObjectMapper映射器=新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 i = 0; I< userDetails.getUser_user()大小();我++)
{
的System.out.println(userF.getId());
}
-
从上面的code我无法从
User_user
获得编号。 -
我如何解析和。
我改的 列表<> user_user;
以列表< User_user > user_user;在的UserDetails 结果
由Michał Ziober
之后,我能够轻松地检索所有的结果,这里是code,结果没有那些 SYSOUT 骨节病>它几乎没有 3行
。
的System.out.println(userDetails.getCity()); //市,名字,姓氏
的for(int i = 0; I< userDetails.getUser_user()大小();我++){
的System.out.println(userDetails.getUser_user()获得(ⅰ).getId()); // ID,域名
尝试{
为(中间体K = 0; K&下; userDetails.getUser_user()获得(ⅰ).getFav_colors()大小();说明k ++){
的System.out.println(userDetails.getUser_user()获得(ⅰ).getFav_colors()得到(k)的); // fav_color
}
}
赶上(例外五){
的System.out.println(no最爱颜色);
}
}
有关我已经加入GitHub的中中的项目。参照
Am using Jackson JSON Processor to get details form JSON, I have the 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 i = 0; i < userDetails.getUser_user().size(); i++)
{
System.out.println(userF.getId());
}
From the above code am not able to get Id from
User_user
.How do I parse and get
fav_colors
if my JSON had array again.
I changed List<?> user_user;
to List<User_user> user_user; in UserDetails
as suggested by Michał Ziober
After that I was able to retrieve all results easily, here is the code.
Without those it's hardly 3 lines
.
System.out.println(userDetails.getCity()); //City, FirstName, LastName
for(int i = 0; i < 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 a reference I have added the project in GitHub.
这篇关于解析JSON对象数组里面杰克逊的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!