java中json字符串怎么转json对象
StringjsonStr="[{'id':'11','parentId':'root','refObj':{'existType':'exist','deptType'...
String jsonStr ="[{'id':'11','parentId':'root','refObj':{'existType':'exist','deptType':'emp','treeNodeType':'dept'}]"
java中怎么去获取jsonStr中的refObj里的existType的值 展开
java中怎么去获取jsonStr中的refObj里的existType的值 展开
4个回答
展开全部
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import java.util.List;
public class Test {
public static void main(String[] args) {
String jsonStr = "[{'id':'11','parentId':'root','refObj':{'existType':'exist','deptType':'emp','treeNodeType':'dept'}}]";
List<JSONObject> list = JSON.parseArray(jsonStr, JSONObject.class);
for (JSONObject object : list) {
System.out.println(object.getJSONObject("refObj").getString("deptType"));
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public static void test4() {
String jsonStr = "[{'id':'11','parentId':'root','refObj':{'existType':'exist','deptType':'emp','treeNodeType':'dept'}}]";
Gson gson = new Gson();
Type type = new TypeToken<List<Bean>>() {
}.getType();
List<Bean> list = gson.fromJson(jsonStr, type);
for (int i = 0; i < list.size(); i++) {
Bean bean = list.get(i);
// 这就是你需要的值
String existVal = bean.getRefObj().getExistType();
System.out.println(existVal);
}
}
class Bean {
private String id;
private String parentId;
private RefObj refObj;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getParentId() {
return parentId;
}
public void setParentId(String parentId) {
this.parentId = parentId;
}
public RefObj getRefObj() {
return refObj;
}
public void setRefObj(RefObj refObj) {
this.refObj = refObj;
}
@Override
public String toString() {
return "Bean [id=" + id + ", parentId=" + parentId + ", refObj=" + refObj + "]";
}
}
class RefObj {
private String existType;
private String deptType;
private String treeNodeType;
public String getExistType() {
return existType;
}
public void setExistType(String existType) {
this.existType = existType;
}
public String getDeptType() {
return deptType;
}
public void setDeptType(String deptType) {
this.deptType = deptType;
}
public String getTreeNodeType() {
return treeNodeType;
}
public void setTreeNodeType(String treeNodeType) {
this.treeNodeType = treeNodeType;
}
@Override
public String toString() {
return "RefObj [existType=" + existType + ", deptType=" + deptType + ", treeNodeType=" + treeNodeType + "]";
}
}
另外,你的json有错哦!在后面少了一个‘}’
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2015-11-18
展开全部
String jsonStr ="{'id':'11','parentId':'root','refObj':{'existType':'exist','deptType':'emp','treeNodeType':'dept'}}";
JSONObject jsonObj = new JSONObject(jsonStr);
JSONObject refObj = new JSONObject(jsonObj.getString("refObj"));
String existType = refObj.getString("existType");
System.out.println(existType);
jar使用的是org.json.jar
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询