Posted in Java/Android onJune 16, 2022
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);
/* jsonString为String类型的*/
/*{"totalSpace":500,"parkName":"天健物业","restSpace":500,"parkCode":"tjwy"}*/
//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);
以上为个人经验,希望能给大家一个参考,也希望大家多多支持三水点靠木。
springboot实现string转json json里面带数组
- Author -
胖虎艾春辉- Original Sources -
声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Tags in this post...
Reply on: @reply_date@
@reply_contents@