我的目标是
String obj = " {\"userNames\":[\"aaaaa aaaaa\",\"Anders Ohm\",\"Arthur Hansen\"]}";
需要将obj转换为java List对象,其中包含带有元素的用户名的详细信息 " aaaaa aaaaa"," Anders Ohm"," Arthur Hansen"
答案 0 :(得分:0)
您可以使用jackson库进行json处理。
使用ObjectMapper,您的JSON可以像这样解析:
class Data {
List<String> userNames = new ArrayList<>();
public List<String> getUserNames() {
return userNames;
}
public void setUserNames( List<String> userNames ) {
this.userNames = userNames;
}
}
public static void main( String[] args ) throws IOException
{
String obj = " {\"userNames\":[\"aaaaa aaaaa\",\"Anders Ohm\",\"Arthur Hansen\"]}";
ObjectMapper mapper = new ObjectMapper();
Data data = mapper.readValue( obj, Data.class );
System.out.println(data.getUserNames());
}
答案 1 :(得分:0)
首先你需要创建类
public class UserNames {
private List<String> userNames;
public List<String> getUserNames() {
return userNames;
}
public void setUserNames(List<String> userNames) {
this.userNames = userNames;
}
}
然后将其解析为
String obj = " {\"userNames\":[\"aaaaa aaaaa\",\"Anders Ohm\",\"Arthur Hansen\"]}";
UserNames userNames =gson.fromJson(obj, UserNames.class);
List<String> names = userNames.getUserNames();
您可以从此处下载Google gson