java中json字符串怎么转json对象
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java中json字符串怎么转json对象相关的知识,希望对你有一定的参考价值。
String jsonStr ="['id':'11','parentId':'root','refObj':'existType':'exist','deptType':'emp','treeNodeType':'dept']"
java中怎么去获取jsonStr中的refObj里的existType的值
1、将jsonstr转为json对象 (这个可以用net.sf.json.JSONObject第三方包来实现)
2、根据refObj 这个key获取'existType':'exist','deptType':'emp','treeNodeType':'dept',把获取的这数据再转为json.
3、将转后的json根据existType就能取得值了 。 参考技术A 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"));
参考技术B 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有错哦!在后面少了一个‘’
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本回答被提问者采纳
以上是关于java中json字符串怎么转json对象的主要内容,如果未能解决你的问题,请参考以下文章