package com.test;
import java.text.ParseException;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONObject;
public class Utili {
public static void Json2Obj(String s) {
JSONArray array;
try {
array = new JSONArray(s);
} catch (ParseException e) {
e.printStackTrace();
return;
}
StringBuilder sb = new StringBuilder("\r\r----------------------\r");
for (int i = 0; i < array.length(); i++) {
JSONObject obj = array.getJSONObject(i);
sb.append("id:").append(obj.getInt("id")).append("\r");
sb.append("name:").append(obj.getString("name")).append("\r");
sb.append("gender:").append(obj.getString("gender")).append("\r");
sb.append("email:").append(obj.getString("email")).append("\r");
sb.append("----------------------\r");
}
System.out.println(sb.toString());
}
public static String Obj2Json() {
ArrayList<User> list = new ArrayList<User>();
User bean1;
for (int i = 0; i < 10; i++) {
bean1 = new User();
bean1.setId(i);
bean1.setName("kilo_" + i);
bean1.setEmail("kilonet@kilonet.cn");
bean1.setGender("male");
list.add(bean1);
}
JSONArray array = new JSONArray();
for (User bean : list) {
JSONObject obj = new JSONObject();
try {
obj.put("id", bean.getId());
obj.put("name", bean.getName());
obj.put("email", bean.getEmail());
obj.put("gender", bean.getGender());
} catch (Exception e) {
}
array.put(obj);
}
System.out.print(array.toString());
return array.toString();
}
public static class User {
private int id;
private String name;
private String email;
private String gender;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
}
import java.text.ParseException;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONObject;
public class Utili {
public static void Json2Obj(String s) {
JSONArray array;
try {
array = new JSONArray(s);
} catch (ParseException e) {
e.printStackTrace();
return;
}
StringBuilder sb = new StringBuilder("\r\r----------------------\r");
for (int i = 0; i < array.length(); i++) {
JSONObject obj = array.getJSONObject(i);
sb.append("id:").append(obj.getInt("id")).append("\r");
sb.append("name:").append(obj.getString("name")).append("\r");
sb.append("gender:").append(obj.getString("gender")).append("\r");
sb.append("email:").append(obj.getString("email")).append("\r");
sb.append("----------------------\r");
}
System.out.println(sb.toString());
}
public static String Obj2Json() {
ArrayList<User> list = new ArrayList<User>();
User bean1;
for (int i = 0; i < 10; i++) {
bean1 = new User();
bean1.setId(i);
bean1.setName("kilo_" + i);
bean1.setEmail("kilonet@kilonet.cn");
bean1.setGender("male");
list.add(bean1);
}
JSONArray array = new JSONArray();
for (User bean : list) {
JSONObject obj = new JSONObject();
try {
obj.put("id", bean.getId());
obj.put("name", bean.getName());
obj.put("email", bean.getEmail());
obj.put("gender", bean.getGender());
} catch (Exception e) {
}
array.put(obj);
}
System.out.print(array.toString());
return array.toString();
}
public static class User {
private int id;
private String name;
private String email;
private String gender;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
}
jar包:json包
id 博主 = [[KILONET.CNBLOGS.COM alloc] initWithValue:@"天堂向右,我依然向左"
网名:@"老舟"
兴趣:@"影音,阅读"
动态:@"系统架构设计,Android通信模块开发"
网址:@"http://kilonet.cnblogs.com"
签名:@"--------------------------------------------------
Stay Hungry , Stay Foolish
求 知 若 渴,处 事 若 愚
--------------------------------------------------"
]; // Never Release