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 文件中加入以下依赖即可。
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>