无法通过Java迭代并获取字符串

时间:2014-08-21 10:54:47

标签: java json

我试图使用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)

3 个答案:

答案 0 :(得分:4)

变化:

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

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

amount是一个数值。

答案 1 :(得分:2)

您正在尝试从JSON中检索String,但它实际上是int。查看amount的编码与您在哪里没有引号的区别,以及quantityamountintquantityString

几乎可以肯定,他们都应该是int。您应该查看整个JSON字符串并检查您想要String的值(例如sellerYardName)以及哪些值应为int s(例如quantity )。您应该将始终的任何内容视为数字的数字。

答案 2 :(得分:1)

修改您的代码,如下所示:

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);

}

这应该可以正常工作。