java中json数据_Java中json的使用 解析json数据

1.java提取json嵌套多层的数据

json数据为:

{

"res": [{

"name": "0",

"text": "爱爱情是什么",

"box": [461.4110410834098, 8.025496946344486, 604.3264999327573, 8.968343496450242, 604.0889589165902, 44.97450305365551, 461.1735000672427, 44.031656503549755]

},

{

"name": "1",

"text": "作者:张兴旺来源:文章阅读网时间:2019-01-1015:59阅读:22412字体大小:四伐][内",

"box": [75.45250280010004, 50.99561949941068, 997.5825041353837, 52.00439964880648, 997.5474971998999, 84.00438050058932, 75.41749586461634, 82.99560035119352]

},

{

"name": "2",

"text": "岁月下动人的温暖。一般来说,爱情的质量取决于相爱者的灵魂的质量。",

"box": [ - 0.30999999999994543, 804.0, 728.31, 804.0, 728.31, 838.0, -0.30999999999994543, 838.0]

}],

"timeTake": 14.2702

}

操作代码为:

import com.alibaba.fastjson.JSONObject;

import com.alibaba.fastjson.JSONArray;

public static void main(String[] args) {

String str2="{\"res\": [{\"name\": \"0\", \"text\": \"爱爱情是什么\", \"box\": [461.4110410834098, 8.025496946344486, 604.3264999327573, 8.968343496450242, 604.0889589165902, 44.97450305365551, 461.1735000672427, 44.031656503549755]}, {\"name\": \"1\", \"text\": \"作者:张兴旺来源:文章阅读网时间:2019-01-1015:59阅读:22412字体大小:四伐][内\", \"box\": [75.45250280010004, 50.99561949941068, 997.5825041353837, 52.00439964880648, 997.5474971998999, 84.00438050058932, 75.41749586461634, 82.99560035119352]}, {\"name\": \"2\", \"text\": \"岁月下动人的温暖。一般来说,爱情的质量取决于相爱者的灵魂的质量。\", \"box\": [-0.30999999999994543, 804.0, 728.31, 804.0, 728.31, 838.0, -0.30999999999994543, 838.0]}], \"timeTake\": 14.2702}";

System.out.println(str2);

JSONObject jsonObject = JSONObject.parseObject(str2);

// System.out.println(jsonObject);

String textString = "";

JSONArray jsonArray = jsonObject.getJSONArray("res");

for (int i = 0; i < jsonArray.size(); i++) {

String text = jsonArray.getJSONObject(i).getString("text");

// System.out.println("text:" + text);

textString += text + "\n";

}

System.out.println(textString);

String timeTake = jsonObject.getString("timeTake");

System.out.println("timeTake:" + timeTake);

}

运行结果

{"res": [{"name": "0", "text": "爱爱情是什么", "box": [461.4110410834098, 8.025496946344486, 604.3264999327573, 8.968343496450242, 604.0889589165902, 44.97450305365551, 461.1735000672427, 44.031656503549755]}, {"name": "1", "text": "作者:张兴旺来源:文章阅读网时间:2019-01-1015:59阅读:22412字体大小:四伐][内", "box": [75.45250280010004, 50.99561949941068, 997.5825041353837, 52.00439964880648, 997.5474971998999, 84.00438050058932, 75.41749586461634, 82.99560035119352]}, {"name": "2", "text": "岁月下动人的温暖。一般来说,爱情的质量取决于相爱者的灵魂的质量。", "box": [-0.30999999999994543, 804.0, 728.31, 804.0, 728.31, 838.0, -0.30999999999994543, 838.0]}], "timeTake": 14.2702}

爱爱情是什么

作者:张兴旺来源:文章阅读网时间:2019-01-1015:59阅读:22412字体大小:四伐][内

岁月下动人的温暖。一般来说,爱情的质量取决于相爱者的灵魂的质量。

timeTake:14.2702

2.所用到的jar包

无需Maven :

https://blog.csdn.net/bingocoder/article/details/89191847

在 Maven 构建的项目中,在 pom.xml 文件中加入以下依赖即可。

net.sf.json-libgroupId>

json-libartifactId>

2.4version>

jdk15classifier>

dependency>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值