Search code examples
javaandroidandroid-studioandroid-jsongson

Problems converting JSONObject to JSONArray


so my application have one small error preventing me from doing currency conversion.

03-01 03:56:05.810 18853-18870/com.example.justin.currencyconverter20 D/JSON Parser: result: {"base":"SGD","date":"2016-02-29","rates":{"AUD":0.99576,"BGN":1.2762,"BRL":2.8316,"CAD":0.96359,"CHF":0.71217,"CNY":4.6559,"CZK":17.655,"DKK":4.868,"GBP":0.51276,"HKD":5.5237,"HRK":4.9764,"HUF":203.11,"IDR":9500.4,"ILS":2.7769,"INR":48.537,"JPY":80.352,"KRW":879.31,"MXN":12.92,"MYR":2.9931,"NOK":6.2018,"NZD":1.0804,"PHP":33.68,"PLN":2.8413,"RON":2.9205,"RUB":53.927,"SEK":6.0828,"THB":25.336,"TRY":2.1059,"USD":0.71047,"ZAR":11.391,"EUR":0.65253}}
03-01 03:56:05.811 18853-18870/com.example.justin.currencyconverter20 E/JSON Parser: Error parsing data org.json.JSONException: Value {"AUD":0.99576,"BGN":1.2762,"BRL":2.8316,"CAD":0.96359,"CHF":0.71217,"CNY":4.6559,"CZK":17.655,"DKK":4.868,"GBP":0.51276,"HKD":5.5237,"HRK":4.9764,"HUF":203.11,"IDR":9500.4,"ILS":2.7769,"INR":48.537,"JPY":80.352,"KRW":879.31,"MXN":12.92,"MYR":2.9931,"NOK":6.2018,"NZD":1.0804,"PHP":33.68,"PLN":2.8413,"RON":2.9205,"RUB":53.927,"SEK":6.0828,"THB":25.336,"TRY":2.1059,"USD":0.71047,"ZAR":11.391,"EUR":0.65253} at rates of type org.json.JSONObject cannot be converted to JSONArray

And here is my JSONParser:

package com.example.justin.currencyconverter20;

import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;

public class JSONParser {

    String charset = "UTF-8";
    HttpURLConnection conn;
    DataOutputStream wr;
    StringBuilder result;
    URL urlObj;
    JSONArray Obj = null;
    StringBuilder sbParams;
    String paramsString;


    public JSONArray makeHttpRequest(String url, String method) {

        int i = 0;

        if(method.equals("GET")){

            try {
                urlObj = new URL(url);

                conn = (HttpURLConnection) urlObj.openConnection();

                conn.setDoOutput(false);

                conn.setRequestMethod("GET");

                conn.setConnectTimeout(15000);

                conn.connect();

            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        try {
            //Receive the response from the server
            InputStream in = new BufferedInputStream(conn.getInputStream());
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            result = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                result.append(line);
            }

            Log.d("JSON Parser", "result: " + result.toString());

        } catch (IOException e) {
            e.printStackTrace();
        }

        conn.disconnect();

        // try parse the string to a JSON object
        try {
            JSONObject jObj = new JSONObject(result.toString());
            Obj = jObj.getJSONArray("rates");
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON Object
        return Obj;
    }
}

Any help would be appreciated. Note: I am still fairly new to this so please simplify if possible, thank you so much.

EDIT: Ok, the JSON api I'm using is http://api.fixer.io/latest?base=SGD and I converted it into JSON format using www.jsonlint.com into the following JSON:

{
    "base": "SGD",
    "date": "2016-02-29",
    "rates": {
        "AUD": 0.99576,
        "BGN": 1.2762,
        "BRL": 2.8316,
        "CAD": 0.96359,
        "CHF": 0.71217,
        "CNY": 4.6559,
        "CZK": 17.655,
        "DKK": 4.868,
        "GBP": 0.51276,
        "HKD": 5.5237,
        "HRK": 4.9764,
        "HUF": 203.11,
        "IDR": 9500.4,
        "ILS": 2.7769,
        "INR": 48.537,
        "JPY": 80.352,
        "KRW": 879.31,
        "MXN": 12.92,
        "MYR": 2.9931,
        "NOK": 6.2018,
        "NZD": 1.0804,
        "PHP": 33.68,
        "PLN": 2.8413,
        "RON": 2.9205,
        "RUB": 53.927,
        "SEK": 6.0828,
        "THB": 25.336,
        "TRY": 2.1059,
        "USD": 0.71047,
        "ZAR": 11.391,
        "EUR": 0.65253
    }
}

Solution

  • There's no JSON Array in your json string. So you have to get another JSON Object.

    JSONObject ratesObject = jObj.getJSONIObject("rates");
    

    Then you can easily get values as following...

    Double aud = ratesObject.getDouble("AUD");
    Double bgn = ratesObject.getDouble("BGN");