json解析

来源:互联网 发布:化妆品可以去淘宝买吗 编辑:程序博客网 时间:2024/06/06 05:51

用于两组数据相同下解析

public class JsonUtils {

public void parseJson(String jsonData){
Type listType = new TypeToken<LinkedList<User>>(){}.getType();
Gson gson=new Gson();
LinkedList<User> users=gson.fromJson(jsonData, listType);
for(Iterator iterator=users.iterator();iterator.hasNext();){
User user=(User)iterator.next();
System.out.println("name--->"+user.getName());
System.out.println("age---->"+user.getAge());
}

}

public class User {
private String name;
private int age;
public User(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}

}



public class MainActivity extends Activity {
private Button button=null;
private String jsonDate="[{\"name\":\"zhangsan\",\"age\":\"20\"},{\"name\":\"lisi\",\"age\":28}]";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=(Button) this.findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
JsonUtils jsonUtils=new JsonUtils();
jsonUtils.parseJson(jsonDate);

}
});
}

}


0 0
原创粉丝点击