**需要导入第三方jar包:gson-2.2.1.jar
package demo06;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
public class Test03 {
/**
* @param args
*/
public static void main(String[] args) {
String json = "[{name:'张三',scores:"
+ "{en:90,math:99,cn:89}}, {name:'李四',scores:{en:80,math:90,cn:99}}]";
TypeToken<List<User>>type=new TypeToken<List<User>>(){};
Gson gson=new Gson();
List<User>list=gson.fromJson(json,type.getType());
for(User u:list){
System.out.println(u);
}
}
}
class Score{
int en;
int math;
int cn;
@Override
public String toString() {
return "Score [en=" + en + ", math=" + math + ", cn=" + cn + "]";
}
}
class User{
String name;
Score scores;
@Override
public String toString() {
return "User [name=" + name + ", scores=" + scores + "]";
}
}
共同学习,写下你的评论
评论加载中...
作者其他优质文章