I'm trying to iterate and get the strings using Java. But, however I'm not able to get it. Here is my code
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)
Change:
String name1 = item.getString("amount");
to
String name1 = item.getInteger("amount");
amount
is a numeric value.
You're trying to retrieve a String
from the JSON, but it's actually an int
. Look at the difference between your encoding of the amount
, where you don't have quotes round the value, and quantity
, where you do. The amount
is an int
, and the quantity
is a String
.
Almost certainly they should both be int
s. You should look through your whole JSON string and check which values you want to be String
s (eg, sellerYardName
) and which ones should be int
s (eg, quantity
). You should treat anything that's always going to be numeric as something numeric.
Modify your code as follows:
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.getInteger("amount");
System.out.println("Name:" + name1);
}
This should work fine.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.