Exorcist
Exorcist

Reputation: 252

Parsing an JSON file from webpage and storing the values in 2d array

For my currency converter app I need to get the latest exchange rates from net and a fellow StackOverflow user suggested me an awesome API for the same which uses JSON file format. Since,I didn't know a thing about JSON I looked up at most of the tutorials which could help but all I could get was creating an JSON File and then Parsing it.

The JSON file is as shown and for simplicity lets consider its url as "http://a.json" -

{
"disclaimer": "This data is collected from various providers and provided free of charge for informational purposes only, with no guarantee whatsoever of accuracy, validity, availability or fitness for any purpose; use at your own risk. Other than that - have fun, and please share/watch/fork if you think data like this should be free!",
"license": "Data collected from various providers with public-facing APIs; copyright may apply; not for resale; no warranties given.",
"timestamp": 1332601928,
"base": "USD",
"rates": {
    "AED": 3.6732,
    "AFN": 48.27,
    "ALL": 105.459999,
    "AMD": 387.929993,
    "ANG": 1.79,
    "AOA": 95.305,
    "ARS": 4.37,
    "AUD": 0.954928,
    "AWG": 1.7899,
    "AZN": 0.7863,
    "BAM": 1.47695,
    "BBD": 2,
    "BDT": 81.779999,
    "BGN": 1.482,
    "BHD": 0.37703,
    "BIF": 1305,
    "BMD": 1,
    "BND": 1.26095,
    "BOB": 6.91,
    "BRL": 1.8107,
    "BSD": 1,
    "BTN": 51.174999,
    "BWP": 7.2751,
    "BYR": 8050,
    "BZD": 1.9135,
    "CAD": 0.99838,
    "CDF": 924.684204,
    "CHF": 0.9083,
    "CLF": 0.02173,
    "CLP": 489.25,
    "CNY": 6.3086,
    "COP": 1760.5,
    "CRC": 504.700012,
    "CUP": 1,
    "CVE": 83.32,
    "CZK": 18.544901,
    "DJF": 179.470001,
    "DKK": 5.60379,
    "DOP": 39.025002,
    "DZD": 74.144997,
    "EGP": 6.0377,
    "ETB": 17.465,
    "EUR": 0.753523,
    "FJD": 1.7797,
    "FKP": 0.6301,
    "GBP": 0.63012,
    "GEL": 1.63875,
    "GHS": 1.7625,
    "GIP": 0.63005,
    "GMD": 29.700001,
    "GNF": 7102.5,
    "GTQ": 7.7565,
    "GYD": 202.649994,
    "HKD": 7.76787,
    "HNL": 19.055,
    "HRK": 5.6717,
    "HTG": 41,
    "HUF": 220.970001,
    "IDR": 9183,
    "IEP": 0.593507,
    "ILS": 3.7293,
    "INR": 51.217548,
    "IQD": 1165.5,
    "IRR": 12278,
    "ISK": 126.080002,
    "JMD": 86.699997,
    "JOD": 0.7085,
    "JPY": 82.345001,
    "KES": 83.074997,
    "KGS": 46.798801,
    "KHR": 4005,
    "KMF": 370.750763,
    "KPW": 900,
    "KRW": 1135.449951,
    "KWD": 0.2785,
    "KZT": 147.774994,
    "LAK": 7988.799805,
    "LBP": 1504.5,
    "LKR": 130.100006,
    "LRD": 73.510002,
    "LSL": 7.7093,
    "LTL": 2.6016,
    "LVL": 0.525,
    "LYD": 1.2616,
    "MAD": 8.4153,
    "MDL": 11.827,
    "MGA": 2135,
    "MKD": 46.52,
    "MMK": 6.51,
    "MNT": 1312.5,
    "MOP": 8.00105,
    "MRO": 294,
    "MUR": 29.139999,
    "MVR": 15.37,
    "MWK": 165.998093,
    "MXN": 12.7545,
    "MYR": 3.077,
    "MZN": 27.434999,
    "NAD": 7.72,
    "NGN": 157.699997,
    "NIO": 23.235001,
    "NOK": 5.75215,
    "NPR": 80.830002,
    "NZD": 1.221032,
    "OMR": 0.38425,
    "PAB": 1,
    "PEN": 2.67,
    "PGK": 2.0602,
    "PHP": 42.959999,
    "PKR": 91.910004,
    "PLN": 3.1285,
    "PYG": 4295,
    "QAR": 3.6418,
    "RON": 3.2943,
    "RSD": 83.780502,
    "RUB": 29.2896,
    "RWF": 607.127014,
    "SAR": 3.7505,
    "SBD": 7.070963,
    "SCR": 14.0244,
    "SDG": 2.675,
    "SEK": 6.7333,
    "SGD": 1.262,
    "SHP": 0.63005,
    "SLL": 4359.5,
    "SOS": 1627,
    "SRD": 3.2875,
    "STD": 18500,
    "SVC": 8.7475,
    "SYP": 57.450001,
    "SZL": 7.7162,
    "THB": 30.695,
    "TJS": 4.7592,
    "TMT": 2.8475,
    "TND": 1.5143,
    "TOP": 1.693601,
    "TRY": 1.8017,
    "TTD": 6.41865,
    "TWD": 29.577999,
    "TZS": 1592,
    "UAH": 8.0265,
    "UGX": 2495,
    "USD": 1,
    "UYU": 19.530001,
    "UZS": 1839.400024,
    "VEF": 4.2951,
    "VND": 20856,
    "VUV": 90.400002,
    "WST": 2.247475,
    "XAF": 494.334412,
    "XCD": 2.7,
    "XDR": 0.648628,
    "XOF": 496.100006,
    "XPF": 90.349998,
    "YER": 215.604996,
    "ZAR": 7.6874,
    "ZMK": 5335,
    "ZWL": 322.355011
}
}

Please suggest for the same.

This is what I am thinking - can store the rates in an 2D String array and retrieve the same whenever required. But I couldn't find anything for an 2D JSONArray parser. So,any help would be greatly appreciated.

Thanking you in anticipation.

Upvotes: 1

Views: 1066

Answers (4)

jmishra
jmishra

Reputation: 2081

I ran into this issue few days ago and figured it out. When you receive them via web request you can do the following.

   String uri="http://data.someSite.api/path/to/the/file.json";               
   HttpGet get = new HttpGet(uri);
   HttpClient client = new DefaultHttpClient();
   HttpResponse response = client.execute(get);
   BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
   StringBuilder builder=new StringBuilder();
  for(String line=null;(line = reader.readLine()) != null;){
  builder.append(line).append("\n");
}
  JSONTokener jsonTokener=new JSONTokener(builder.toString());
  JSONObject finalJson=new JSONObject(jsonTokener);

In your case if you want to find the AUD you do

JSONObject aussieCurrency=finalJson.getJSONObject("AUD");

Upvotes: 1

tjb
tjb

Reputation: 11728

It ain't pretty, but if you're in a hurry...

public String[][] run() {
        // TODO Auto-generated method stub
        java.io.InputStream r = getClass().getResourceAsStream("input.json");

        getClass().getResourceAsStream("input.json");
        Writer writer = new StringWriter();
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(r,"UTF-8"));
            char[] buffer = new char[1024];
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
                }

            JSONObject jObj = new JSONObject(writer.toString());
            String rates = jObj.getString("rates");
            rates=rates.substring(1, rates.length()-1);
            String[] rates2 = rates.split(",");
            String[] rate;
            String[][] finished = new String[rates2.length][2];
            for(int i=0;i<rates2.length;i++){
                finished[i]=rates2[i].split(":");
            }
            return finished;
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;

    }

and of course the imports are from json.org

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

Upvotes: 1

Cacho Santa
Cacho Santa

Reputation: 6914

You should try the Google Library Gson.

Gson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object.

You will only need to create a Java Bean that represents your data(your 2D array could be a HashMap). After that you will only need to parse, there are a lot of examples in StackOverflow about that.

You can try this link also. I hope it helps!

Upvotes: 0

Shubhayu
Shubhayu

Reputation: 13552

JSON Parsing is pretty simple. Read up on JSONObjects and also JSONArray. Here is something I found recently. Its very brief without much explanation. (which i plan to correct in a tutorial I'll be writing soon). WIll post a link here once I am done. Till then check this link out. Might help.

http://www.jondev.net/articles/Android_JSON_Parser_Example

Upvotes: 0

Related Questions