springboot实现string转jsonjson里面带数组
string转json json里面带数组
json示例
[
{source:0,target:4,relation:"籍贯",value:1.3},
{source:4,target:5,relation:"舍友",value:1},
{source:4,target:6,relation:"舍友",value:1},
{source:4,target:7,relation:"舍友",value:1},
{source:1,target:6,relation:"籍贯",value:2},
{source:2,target:5,relation:"籍贯",value:0.9},
{source:3,target:7,relation:"籍贯",value:1},
{source:5,target:6,relation:"同学",value:1.6},
{source:6,target:7,relation:"朋友",value:0.7},
{source:6,target:8,relation:"职责",value:2}
]
我的需求是将string类型转化为json数组
添加依赖
<!--string类型转化成json数组并输出-->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
使用这个包进行解析
String string = " [\n" +
" {source:0,target:4,relation:\"籍贯\",value:1.3},\n" +
" {source:4,target:5,relation:\"舍友\",value:1},\n" +
" {source:4,target:6,relation:\"舍友\",value:1},\n" +
" {source:4,target:7,relation:\"舍友\",value:1},\n" +
" {source:1,target:6,relation:\"籍贯\",value:2},\n" +
" {source:2,target:5,relation:\"籍贯\",value:0.9},\n" +
" {source:3,target:7,relation:\"籍贯\",value:1},\n" +
" {source:5,target:6,relation:\"同学\",value:1.6},\n" +
" {source:6,target:7,relation:\"朋友\",value:0.7},\n" +
" {source:6,target:8,relation:\"职责\",value:2}\n" +
"]";
JSONArray jsonArray = JSONArray.fromObject(string);
string转json简单方法
String jsonString= queryParkSpaceService.execute(params);
//String转json
JSONObject kkk=new JSONObject();
kkk.put("json0", jsonString);
//取值
String restSpace=kkk.getJSONObject("json0").getString("restSpace");
String totalSpace = kkk.getJSONObject("json0").getString("totalSpace");
String parkName = kkk.getJSONObject("json0").getString("parkName");
String parkCode = kkk.getJSONObject("json0").getString("parkCode");
//合并
JSONObject jsonObj = new JSONObject();
jsonObj.put("restSpace", restSpace);
jsonObj.put("totalSpace", totalSpace);
jsonObj.put("parkCode", parkCode);
jsonObj.put("parkName", parkName);
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。
相关文章