-2

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
    }
}
Community
  • 1
  • 1

4 Answers4

1

if rates is a array then code is bellow:

 JSONArray Objs = jObj.getJSONArray("rates");

but in your string is a JSON Object "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
}

so use code bellow:

JSONObe Obj = jObj.getJSONObject("rates");

and you can access any field of Obj by:

Double php = Obj.getDouble("PHP");

or get to String type:

String php = Obj.getString("PHP");
Bhargav
  • 8,118
  • 6
  • 40
  • 63
GiapLee
  • 436
  • 2
  • 8
0

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");
User
  • 4,023
  • 4
  • 37
  • 63
  • // 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()); } in this try block – User Mar 01 '16 at 04:31
  • Hmm, after doing that I got this error: 03-01 04:37:22.960 19287-19335/com.example.justin.currencyconverter20 E/JSON Parser: Error parsing data org.json.JSONException: No value for RUN – OnaniMaster Mar 01 '16 at 04:42
  • yes of course.. because you dont have the key "RUN" in your JSON. It is "RON" i can see. – User Mar 01 '16 at 04:45
0

Try this.

public JSONObject makeHttpRequest(String url, String method) {
 try {
            JSONObject jObj = new JSONObject(result.toString());
             JSONObject jObj2 = jObj.getJSONObject("rates");
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }   
return jObj2; 
}
Pitty
  • 1,907
  • 5
  • 16
  • 34
0

Firstly, When trying to parse Json formatted data, you should be able to differentiate the JsonObjects and JsonArrays. There are many tools for this, but I recommend the :

http://jsonviewer.stack.hu/

Secondly, you can use libraries like Gson to convert the json formatted data in to java POJO classes and objects. For this, you can use the following tool, which will generate the equivalent POJO classes you need to create for using Gson:

http://pojo.sodhanalibrary.com/

Finally, you should make the following change in your code to remove the error :

JSONObject ratesObject = jObj.getJSONIObject("rates");
String aud = ratesObject.getString("AUD");
Farhad
  • 12,178
  • 5
  • 32
  • 60