Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从JSon文件中提取一个特定数据,然后将所有数据相加_Java_Json_Rest - Fatal编程技术网

Java 从JSon文件中提取一个特定数据,然后将所有数据相加

Java 从JSon文件中提取一个特定数据,然后将所有数据相加,java,json,rest,Java,Json,Rest,所以我有点被卡住了。我正在体验JSon,我正在尝试从JSon文件中获取不同订单的所有总价。我可以阅读文件,但我在获取具体信息然后总结这些信息时遇到了问题 这是我到目前为止的代码 public static void main(String[] args) { JSONParser parser = new JSONParser(); try { Object obj = parser.parse(new FileReader(

所以我有点被卡住了。我正在体验JSon,我正在尝试从JSon文件中获取不同订单的所有总价。我可以阅读文件,但我在获取具体信息然后总结这些信息时遇到了问题

这是我到目前为止的代码

public static void main(String[] args) {




        JSONParser parser = new JSONParser();

        try {

            Object obj = parser.parse(new FileReader(
                    "/orders.json"));

            JSONObject jsonObject = (JSONObject) obj;


            JSONArray orderList = (JSONArray) jsonObject.get("orders");

            System.out.println("\norders");
            Iterator<String> iterator = orderList.iterator();
            while (iterator.hasNext()) {

                System.out.println(iterator.next());
            }
            obj = parser.parse(sCurrentLine);
    JSONArray jsonArray = (JSONArray) obj;
    for(obj : jsonArray){
        JSONObject jsonObject = (JSONObject)obj;
        JSONObject realTitle = (JSONObject)jsonObject.get("0");
        String name = (String) realTitle.get("title");
    }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
我得到的错误是for(obj:jsonArray)引起了我的问题,但老实说,我认为我甚至没有走上正确的道路


非常感谢您的帮助

您的代码有明显的编译时错误。在for循环中,您再次尝试执行get(0),这是不正确的

我已经改进了代码

试试这个。其结果如下所示

每个订单的价格数据--{4251070723=179.04,4251070787=14.62}

所有订单的总价--193.66

publicstaticvoidmain(字符串[]args){
JSONParser=新的JSONParser();
Map priceData=new HashMap();
BigDecimal totalPrice=BigDecimal.0;
字符串orderId=null;
字符串orderPrice=null;
试一试{
字符串sCurrentLine=null;
Object obj=parser.parse(新文件读取器(
“orders.json”);
JSONObject JSONObject=(JSONObject)对象;
JSONArray订单列表=(JSONArray)(jsonObject.get(“订单”);
System.out.println(“完整订单列表--”+orderList.toString());
对于(对象单订单:订单列表){
JSONObject singleArrayData=(JSONObject)singleOrder;
orderId=singleArrayData.get(“id”).toString();
orderPrice=singleArrayData.get(“总价”).toString();
priceData.put(orderId,orderPrice);
totalPrice=totalPrice.add(新的BigDecimal(orderPrice));
}
System.out.println(“每个订单的价格数据--”+priceData.toString());
System.out.println(“所有订单的总价--”+总价);
}捕获(例外e){
e、 printStackTrace();
}
}

如果出现错误,请发布完整准确的eror。“是我的问题”并没有告诉我们任何事情。请发布编译的代码。什么是
sCurrentLine
?如果要分析一个文件,为什么要多次调用parse()?
{
"orders":[
{
"id":4251070723,
"email":"heathcote_amya@gmail.com",
"closed_at":null,
"created_at":"2016-12-05T23:16:40-05:00",
"updated_at":"2016-12-05T23:16:40-05:00",
"number":137,
"note":null,
"token":"c460f260f4f38b8a2b1f78e6efd0140e",
"gateway":"",
"test":false,
"total_price":"179.04",
"default":true
},
{
"id":4251070787,
"email":"napoleon.batz@gmail.com",
"closed_at":null,
"created_at":"2016-12-05T23:16:40-05:00",
"updated_at":"2016-12-05T23:16:41-05:00",
"number":138,
"note":null,
"token":"54c4f1735cfec8f98ad16ae5e9a161cd",
"gateway":"",
"test":false,
"total_price":"14.62"
}
]
}
   public static void main(String[] args) {
        JSONParser parser = new JSONParser();
        Map<String, String> priceData = new HashMap<>();
        BigDecimal totalPrice = BigDecimal.ZERO;
        String orderId = null;
        String orderPrice = null;

        try {
            String sCurrentLine = null;

            Object obj = parser.parse(new FileReader(
                    "orders.json"));

            JSONObject jsonObject = (JSONObject) obj;
            JSONArray orderList = (JSONArray)(jsonObject.get("orders"));
            System.out.println("Complete order list --"+orderList.toString());

            for(Object singleOrder : orderList) {
                JSONObject singleArrayData = (JSONObject) singleOrder;
                orderId = singleArrayData.get("id").toString();
                orderPrice = singleArrayData.get("total_price").toString();
                priceData.put(orderId, orderPrice);
                totalPrice = totalPrice.add(new BigDecimal(orderPrice));
            }
            System.out.println("The price data of each order --"+priceData.toString());
            System.out.println("The total Price of all order --"+totalPrice);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }