标签:
//将json格式的字符串转为json对象 var t = JSON.parse(‘{"name":123}‘); alert(t.name);
//json对象数组。 //packJson 为对象数组 packJson = [ {"name":"nikita", "password":"1111"}, {"name":"tony", "password":"2222"} ]; //json to string var s = JSON.stringify(packJson); //S为字符串。 alert(packJson[0].name); //访问对象数组packJson
解析json格式字符串 获得对象
String jsonString = "[" + "{\"author\":\"7\",\"id\":358,\"title\":\"Japan\",\"pictures\":[{\"description\":\"001\",\"imgPath\":\"/cms/u/cms/www/201203/05150720ii68.jpg\"},{\"description\":\"002\",\"imgPath\":\"/cms/u/cms/www/201203/05150720ii67.jpg\"}],\"path\":\"ip\"}," + "{\"author\":\"8\",\"id\":359,\"title\":\"China\",\"pictures\":[{\"description\":\"101\",\"imgPath\":\"/cms/u/cms/www/201203/111111111111.jpg\"},{\"description\":\"102\",\"imgPath\":\"/cms/u/cms/www/201203/222222222222.jpg\"}],\"path\":\"ip\"}]"; JSONArray array = JSONArray.fromObject(jsonString); if (array.size() > 0) { for (int i = 0; i < array.size(); i++) { JSONObject job = array.getJSONObject(i); // 遍历 jsonarray System.out.println("author : " + job.get("author") ); // 得到 每个对象中的属性值 } }
/** * java对象转换成json对象,并获取json对象属性 */ JSONObject jsonStu = JSONObject.fromObject(student); System.out.println(jsonStu.toString()); System.out.println(jsonStu.getJSONArray("hobby"));
/** * json对象转换成java对象,并获取java对象属性 */ Student stu = (Student) JSONObject.toBean(jsonStu, Student.class); System.out.println(stu.getName());
JSONArray array = JSONArray.fromObject(userlist);
String jsonstr = array.toString();
JSONObject object = JSONObject.fromObject(invite);
把JSON字符串转换为JAVA 对象数组
String personstr = “” JSONArray json = JSONArray.fromObject(personstr); List<InvoidPerson> persons = (List<InvoidPerson>)JSONArray.toCollection(json, nvoidPerson.class);
代码来自网络
标签:
原文地址:http://www.cnblogs.com/amibandoufu/p/5327154.html