简体   繁体   中英

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. Still getting the same error.....It difficult to find example of just pulling a jsonObject without an array

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 } }


 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

Problem

JSONArray array = new JSONArray();

Why JSONArray

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

Sol - Use Iterator with 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 ? . It should be double australia .

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

You have no square brackets, so you don't need JSONArray related classes or methods at all.

Use jsonObj2.getString("AUD")

If you actually want numbers, use getDouble("AUD")

Convert string to Json

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

When get String from JSONObject

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

Your rates are in double , but you are calling getString() and store them in int !

Your JsonArray array is empty and you're looping through it!

rates is a 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

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

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.

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