简体   繁体   English

无法解析JsonObject-错误“无法将字符串转换为JSONObject”

[英]Trouble parsing JsonObject - error 'String cannot be converted to JSONObject'

Here is what I tried again but to no avail.... 这是我再次尝试的方法,但无济于事。

I switch String to double and change the values in the hashmap to accommodate doubles as well. 我将String切换为double,并更改哈希图中的值以容纳double。 Still getting the same error.....It difficult to find example of just pulling a jsonObject without an array 仍然遇到相同的错误.....很难找到没有数组就拉jsonObject的示例

What the issue here ??? 这里的问题是什么?

{ "base": "EUR", "date": "2017-08-25", "rates": { "AUD": 1.4919, "BGN": 1.9558, "BRL": 3.7045, "CAD": 1.4769, "CHF": 1.139, "CNY": 7.8596, "CZK": 26.084, "DKK": 7.4391, "GBP": 0.92083, "HKD": 9.2372, "HRK": 7.414, "HUF": 304.68, "IDR": 15758.0, "ILS": 4.2453, "INR": 75.598, "JPY": 129.59, "KRW": 1327.0, "MXN": 20.844, "MYR": 5.0456, "NOK": 9.2278, "NZD": 1.6363, "PHP": 60.327, "PLN": 4.2598, "RON": 4.5983, "RUB": 69.831, "SEK": 9.5053, "SGD": 1.6055, "THB": 39.332, "TRY": 4.108, "USD": 1.1808, "ZAR": 15.549 } } {“ base”:“ EUR”,“ date”:“ 2017-08-25”,“ rates”:{“ AUD”:1.4919,“ BGN”:1.9558,“ BRL”:3.7045,“ CAD”:1.4769, “ CHF”:1.139,“ CNY”:7.8596,“ CZK”:26.084,“ DKK”:7.4391,“ GBP”:0.92083,“ HKD”:9.2372,“ HRK”:7.414,“ HUF”:304.68,“ IDR “:15758.0,” ILS“:4.2453,” INR“:75.598,” JPY“:129.59,” KRW“:1327.0,” MXN“:20.844,” MYR“:5.0456,” NOK“:9.2278,” NZD“: 1.6363,“ PHP”:60.327,“ PLN”:4.2598,“ RON”:4.5983,“ RUB”:69.831,“ SEK”:9.5053,“ SGD”:1.6055,“ THB”:39.332,“ TRY”:4.108, “ USD”:1.1808,“ ZAR”:15.549}}


 if (jsonStr != null) {
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);


                JSONObject jsonObj2 = jsonObj.getJSONObject("rates");


                double australia = jsonObj2.getDouble("AUD");
                double bulgarian = jsonObj2.getDouble("BDN");
                double brazil = jsonObj2.getDouble("BRL");
                double canadian = jsonObj2.getDouble("CAD");
                double swissfrank = jsonObj2.getDouble("CHF");
                double chinnese = jsonObj2.getDouble("CNY");
                double Czech = jsonObj2.getDouble("CZK");
                double Danish = jsonObj2.getDouble("DKK");
                double british = jsonObj2.getDouble("GBP");
                double hongkong = jsonObj2.getDouble("HKD");
                double croatian = jsonObj2.getDouble("HRK");
                double hungarian = jsonObj2.getDouble("HUF");
                double indonesian = jsonObj2.getDouble("IDR");
                double israeli = jsonObj2.getDouble("ILS");
                double indian = jsonObj2.getDouble("INR");
                double japan = jsonObj2.getDouble("JPY");
                double korean = jsonObj2.getDouble("KRW");
                double mexican = jsonObj2.getDouble("MXN");
                double malaysian = jsonObj2.getDouble("MYR");
                double norwegian = jsonObj2.getDouble("NOK");
                double newzealand = jsonObj2.getDouble("NZD");
                double philippino = jsonObj2.getDouble("PHP");
                double polish = jsonObj2.getDouble("PLN");
                double romanian = jsonObj2.getDouble("RON");
                double russian = jsonObj2.getDouble("RUB");
                double swedish = jsonObj2.getDouble("SEK");
                double singapore = jsonObj2.getDouble("SGD");
                double thai = jsonObj2.getDouble("THB");
                double turkish = jsonObj2.getDouble("TRY");
                double usa = jsonObj2.getDouble("USD");
                double southafrican = jsonObj2.getDouble("ZAR");

                // tmp hash map for single contact
                HashMap<String, Double> contact = new HashMap<>();

                // adding each child node to HashMap key => value
                contact.put("AUD", australia);
                contact.put("BDN", bulgarian);
                contact.put("BRL", brazil);
                contact.put("CAD", canadian);
                contact.put("CHF", swissfrank);
                contact.put("CNY", chinnese);
                contact.put("CZK", Czech);
                contact.put("DKK", Danish);
                contact.put("GBP", british);
                contact.put("HKD", hongkong);
                contact.put("HRK", croatian);
                contact.put("HUF", hungarian);
                contact.put("IDR", indonesian);
                contact.put("ILS", israeli);
                contact.put("INR", indian);
                contact.put("JPY", japan);
                contact.put("KRW", korean);
                contact.put("MXN", mexican);
                contact.put("MYR", malaysian);
                contact.put("NOK", norwegian);
                contact.put("NZD", newzealand);
                contact.put("PHP", philippino);
                contact.put("PLN", polish);
                contact.put("RON", romanian);
                contact.put("RUB", russian);
                contact.put("SEK", swedish);
                contact.put("SGD", singapore);
                contact.put("THB", thai);
                contact.put("TRY", turkish);
                contact.put("USA", usa);
                contact.put("ZAR", southafrican);


                // adding contact to contact list

                contactList.add(contact);

String cannot be converted to JSONObject 字符串不能转换为JSONObject

Problem 问题

JSONArray array = new JSONArray();

Why JSONArray 为什么使用JSONArray

"rates": {
        "AUD": 1.4919,
        "BGN": 1.9558,

Sol - Use Iterator with JSONObject#keys Sol-迭代器JSONObject#keys

            Iterator<String>  iteratorObj = jsonObj2.keys();
            ArrayList<String> al_getAllKeys=new ArrayList<String>();
            while (iteratorObj.hasNext())
            {
                String key = iteratorObj.next();
                ..........
                System.out.println("Key_VALUE: " + key + "------>" + jsonObj2.getString(key));

            }

FYI 费耶

Why int australia ? 为什么选择int australia . It should be double australia . 它应该是double australia

double australia = ratesObject.getDouble("AUD"); double australia = ratesObject.getDouble(“ AUD”);

You have no square brackets, so you don't need JSONArray related classes or methods at all. 您没有方括号,因此根本不需要与JSONArray相关的类或方法。

Use jsonObj2.getString("AUD") 使用jsonObj2.getString("AUD")

If you actually want numbers, use getDouble("AUD") 如果您确实需要数字,请使用getDouble("AUD")

Convert string to Json 将字符串转换为Json

JSONObject jsonObj = new JSONObject(cursorUsers.getString("String");

When get String from JSONObject 当从JSONObject获取字符串时

String str=jsonObj.getString("parseObjectName"));

Your rates are in double , but you are calling getString() and store them in int ! 您的费率是getString() double ,但是您正在调用getString()并将它们存储在int

Your JsonArray array is empty and you're looping through it! 您的JsonArray array为空,您正在遍历它!

rates is a jsonObject. rates是一个jsonObject。 No need to loop, just call it and access each rate by providing its key. 无需循环,只需调用它并通过提供其密钥即可访问每个速率。

double aud = jsonObject2.getDouble("AUD");

double bnd = jsonObject2.getDouble("BND");

You can do it using Google's Gson library with simple steps, try below code 您可以通过简单的步骤使用Google的Gson库进行操作,请尝试以下代码

String jsonString = "Your JSON string";

Gson gson = new Gson();

//Convert Json String to Json Object without a POJO class
JsonObject jsonObj = gson.fromJson (jsonString, JsonElement.class).getAsJsonObject();

//Get rates object and convert it to hashmap (For easy access)
HashMap<String, String> ratesMap = gson.fromJson(jsonObj.get("rates").getAsString(), new TypeToken<HashMap<String, String>>(){}.getType());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM