java读取多个json文件,如何从json文件一个接一个地获取多个json数据?

I am trying to get specific value from json file. Here is my json file.

{

"books": ["book1","book2","book3","book4"],

"set_with_ratio": {

"tweaks_es": "7",

"ratio": {

"brand_defined_sets_ratio": {

"default": {

"desktop": {

"16": "85",

"11": "95",

"19": "10",

"12": {

"2": "50"

}

}

},

"rentals": {

"desktop": {

"16": "75",

"11": "45",

"12": {

"2": "10"

}

}

}

}

}

}

}

How can I get books array elements one by one? I tried this.

JSONParser jsonParser = new JSONParser();

JSONObject jsonObject = (JSONObject) jsonParser.parse(jsonData);

JSONArray jsonArray = (JSONArray) jsonObject.get("books");

//Iterating the images of the array

Iterator iterator = jsonArray.iterator();

while(iterator.hasNext()) {

System.out.println(iterator.next());

}

From this I got this error java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.JSONArray

how can I get data for x.set_with_ratio.ratio.brand_defined_sets_ratio.default.desktop.11 this json path. I am trying this code in Java.

JSONParser jsonParser = new JSONParser();

try {

//Parsing the contents of the JSON file

JSONObject jsonObject = (JSONObject) jsonParser.parse(new FileReader("sample.json"));

String id = jsonObject.get("set_with_ratio").toString();

System.out.println(id);

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} catch (ParseException e) {

e.printStackTrace();

}

Here I get the total json file but cannot manage to get expected result.

解决方案

Try this

import org.json.simple.JSONObject;

import org.json.simple.parser.JSONParser;

import org.json.simple.parser.ParseException;

import org.junit.Test;

public class JavaTest {

@Test

public void name() {

JSONParser jsonParser = new JSONParser();

try {

//Parsing the contents of the JSON file

JSONObject jsonObject = (JSONObject) jsonParser.parse("{\n" +

" \"set_with_ratio\": {\n" +

" \"tweaks_es\": \"7\",\n" +

" \"ratio\": {\n" +

" \"brand_defined_sets_ratio\": {\n" +

" \"default\": {\n" +

" \"desktop\": {\n" +

" \"16\": \"85\",\n" +

" \"11\": \"95\",\n" +

" \"19\": \"10\",\n" +

" \"12\": {\n" +

" \"2\": \"50\"\n" +

" }\n" +

" }\n" +

" },\n" +

" \"rentals\": {\n" +

" \"desktop\": {\n" +

" \"16\": \"75\",\n" +

" \"11\": \"45\",\n" +

" \"12\": {\n" +

" \"2\": \"10\"\n" +

" }\n" +

" }\n" +

" }\n" +

" }\n" +

" }\n" +

" }\n" +

"}");

String id = ((JSONObject) ((JSONObject) ((JSONObject) ((JSONObject) ((JSONObject) jsonObject.get("set_with_ratio"))

.get("ratio"))

.get("brand_defined_sets_ratio"))

.get("default"))

.get("desktop"))

.get("11" +

"").toString();

System.out.println(id);

} catch (ParseException e) {

e.printStackTrace();

}

}

}

Result is 95

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值