public static void main(String[] args) {
String jsonString ="{\"name\":\"zhangsan\",\"password\":\"zhangsan123\",\"email\":\"10371443@qq.com\"}";
JSONObject json = JSONObject.fromObject(jsonString);
User user = new User();
user.setName(json.getString("name"));
user.setPassword(json.getString("password"));
user.setEmail(json.getString("email"));
System.out.println(user.toString());
}
String json = <span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">"[{\"name\":\"zhangsan\",\"password\":\"zhangsan123\",\"email\":\"10371443@qq.com\"},</span><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">{\"name\":\"lisi\",\"password\":\"lisi123\",\"email\":\"1435123@qq.com\"}</span><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">]"</span>;
JSONArray jsonArray = JSONArray.fromObject(json);
ArrayList<User> users = new ArrayList<User>();
for (int i = 0; i < jsonArray.size(); i++) {
User userM = new User();
user.setName(jsonArray.getJSONObject(i).getString("name"));
user.setpassword(jsonArray.getJSONObject(i).getString("password"));
user.setEmail(jsonArray.getJSONObject(i).getString("email"));
users.add(user);
}
for (User user : users) {
System.out.println(user.toString());
}
String st = "{\"translation\":[\"爱\"],\"basic\":{\"us-phonetic\":\"lʌv\",\"phonetic\":\"lʌv\",\"uk-phonetic\":\"lʌv\",\"explains\":[\"n. 恋爱;亲爱的;酷爱;喜爱的事物\",\"vt. 喜欢;热爱;爱慕\",\"vi. 爱\",\"n. (Love)人名;(英)洛夫\"]},\"query\":\"love\",\"errorCode\":0,\"web\":[{\"value\":[\"爱情\",\"爱\",\"爱\"],\"key\":\"Love\"},{\"value\":[\"无尽的爱\",\"蓝色生死恋\",\"不了情\"],\"key\":\"Endless Love\"},{\"value\":[\"早恋\",\"青春期恋爱\",\"初恋\"],\"key\":\"puppy love\"}]}";
JSONObject str = JSONObject.fromObject(st); //json数据的最外层肯定是一个对象,参数为字符串
//对象str又分为3部分:translation,basic,web
if(str.has("translation")){
JSONArray tr = str.getJSONArray("translation"); //translation是一个json数组
for(int i = 0 ;i<tr.size();i++){ //对数组元素进行遍历
System.out.println(tr.getString(i)); //因为元素的值是String类型的所以忽的值的方法是getString(index)
}
}
//basic中有数组也有元素
if(str.has("basic")){
JSONObject us = str.getJSONObject("basic"); //对元素进行解析,并输出元素的值
System.out.print("美:[" + us.getString("us-phonetic") + "]\t");
JSONObject uk = str.getJSONObject("basic");
System.out.print("英:[" + us.getString("uk-phonetic") + "]\n");
JSONObject basic = str.getJSONObject("basic"); //先获得basic对象
JSONArray explain = basic.getJSONArray("explains"); //再获得basic对象下的explains数组
for(int i = 0;i<explain.size(); i++){ //对数组元素进行遍历
System.out.println(explain.getString(i));
}
}
if(str.has("web")){ //web是一个数组,每个数组元素又是三个Json对象
System.out.println("拓展:");
JSONArray web = str.getJSONArray("web");
for(int i = 0; i<web.size() ; i++ ){ //但是对象中又嵌套着数组
String t = web.getString(i); //遍历过程将web数组元素赋给String型变量
JSONObject we = JSONObject.fromObject(t); //通过String又得到每个元素的对象
if(we.has("key")){
System.out.print(we.getString("key")+"\t"); //得到对象中的元素
}
if(we.has("value")){
JSONArray value = we.getJSONArray("value");
for(int x = 0 ; x<value.size() ;x++ ){ //遍历对象中嵌套的数组
System.out.print(value.getString(x)); //得到数组元素的值
if(x<value.size()-1){
System.out.print(";");
}
}
}
System.out.println();
}
}
String str = "[{name:'a',value:'aa'},{name:'b',value:'bb'},{name:'c',value:'cc'},{name:'d',value:'dd'}]" ; // 一个未转化的字符串
JSONArray json = JSONArray.fromObject(str ); // 首先把字符串转成 JSONArray 对象
if(json.size()>0){
for(int i=0;i<json.size();i++){
JSONObject job = json.getJSONObject(i); // 遍历 jsonarray 数组,把每一个对象转成 json 对象
System.out.println(job.get("name")+"=") ; // 得到 每个对象中的属性值
}
}
public class JsonExtracter {
public static void main(String[] args) {
String s = "{\"name\":\"a\",\"family\":[\"张三\",\"李四\"]}";
JSONObject jsonObject = JSON.parseObject(s);
//注意:family中的内容带有中括号[],所以要转化为JSONArray类型的对象
JSONArray family = jsonObject.getJSONArray("family");
for (int i = 0; i < family.size(); i++) {
//提取出family中的所有
String s1 = (String) family.get(i);
System.out.println("currentFamily:" + s1);
}
}
如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛