gpt4 book ai didi

java - 无法通过 Java 迭代和获取字符串

转载 作者:行者123 更新时间:2023-11-29 06:33:52 25 4
gpt4 key购买 nike

我正在尝试使用 Java 迭代并获取字符串。但是,但是我无法得到它。这是我的代码

String str = "{\"sellerYardId\":\"9100000J1\",\"sellerYardName\":\"Raichur\",\"buyerBusinessId\":\"000114078712340000011\",\"buyerBusinessName\":\"Vinay Agri Biz1\",\"buyerYardId\":\"9100000O1\",\"buyerId\":\"00001407870935000001\",\"buyerName\":\"Vinay\",\"buyerYardName\":\"Koppal\",\"entryDate\":\"30-08-2014\",\"tradeType\":\"Local\",\"loadingcharge\":\"2435\",\"brokerComminssion\":\"2345\",\"hamaliCharges\":\"45\",\"otherexpeneces\":\"2435\",\"cashAmmount\":\"2345\",\"chequeDetailsTo\":[{\"bankName\":\"saf\",\"accountNumber\":\"435\",\"chequeNo\":\"435\",\"ifsccode\":\"qwre5q3\",\"salechequedate\":\"25-02-2014\",\"chequeAmmount\":\"435\"}],\"commodityList\":[{\"commodityID\":\"101516040000\",\"commodityName\":\"Millet Seeds\",\"quantity\":\"45\",\"units\":\"Kgs\",\"unitRate\":\"2435\",\"amount\":109575,\"mfRate\":\"243\",\"mfAmount\":266267.25,\"totalAmount\":375842.25},{\"commodityID\":\"504055000001\",\"commodityName\":\"Groundnut (Pod)-Groundnut Seeds\",\"quantity\":\"243\",\"units\":\"Kgs\",\"unitRate\":\"2543\",\"amount\":617949,\"mfRate\":\"2543\",\"mfAmount\":15714443.07,\"totalAmount\":16332392.07}],\"caId\":\"\",\"isPosted\":\"0\",\"fileName\":\"\",\"userId\":\"539aba81e4b09cbbbfbf2cf1\"}";
JSONObject obj = new JSONObject(str);
// System.out.println(obj);
JSONArray jsonArray = obj.getJSONArray("commodityList");
// System.out.println("commodity list:" + jsonArray);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject item = jsonArray.getJSONObject(i);
System.out.println("Hey:" + item);

String name1 = item.getString("amount");
System.out.println("Name:" + name1);

}

错误

Hey:{"totalAmount":375842.25,"amount":109575,"quantity":"45","unitRate":"2435","commodityID":"101516040000","units":"Kgs","mfAmount":266267.25,"mfRate":"243","commodityName":"Millet Seeds"}
Exception in thread "main" org.json.JSONException: JSONObject["amount"] not a string.
at org.json.JSONObject.getString(JSONObject.java:658)
at net.vsspl.traderapp.services.PostGresSale.main(PostGresSale.java:721)

最佳答案

改变:

       String name1 = item.getString("amount");

       String name1 = item.getInteger("amount");

amount 是一个数值。

关于java - 无法通过 Java 迭代和获取字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25424529/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com