『壹』 怎樣從java後台獲取json字元串並轉換為json對象輸出
使用json-lib.jar這個工具x0dx0apublic String getJson(Object obj){x0dx0a JSONObject json;x0dx0a json = JSONObject.fromObject(obj);x0dx0a return json.toString();x0dx0a}x0dx0a使用jquery來處理jsonx0dx0a//轉換為json數據 datas可以用ajax從後台獲取上面getJson中的數據x0dx0avar jsonDatas = eval("(" + datas + ")");x0dx0a //循環遍歷數據x0dx0ajQuery.each(jsonDatas, function(item) {x0dx0a//循環x0dx0a});
『貳』 java 含有數組的字元串怎麼轉化為json對象
1、需要json的相關jar包
2、設置json格式字元串:
String str="{\"student\":[{\"name\":\"leilei\",\"age\":23},{\"name\":\"leilei02\",\"age\":23}]}";
3、建立相應的類:
public class StudentList {
List<Student> student;
public List<Student> getStudent() {
return student;
}
public void setStudent(List<Student> student) {
this.student = student;
}
}
public class Student {
private String name;
private int age;
//private StudentClass studentClass;
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;
}
}
『叄』 java中字元串怎麼轉json
string類型如果要轉換成json的話,就需要寫成這樣的形式,如下:x0dx0aString jsonStr ="{'id':ཇ','parentId':'root','refObj':{'existType':'exist','deptType':'emp','treeNodeType':'dept'}}";x0dx0a JSONObject jsonObj = new JSONObject(jsonStr);x0dx0a JSONObject refObj = new JSONObject(jsonObj.getString("refObj"));x0dx0a String existType = refObj.getString("existType");x0dx0a System.out.println(existType);x0dx0ajar使用的是org.json.jar