0

I want to parse this object from a JSON Array:

{..."avg": 8.492619161922457352960767294, "symbol": "mtgoxUSD", "low": 8.391000000000}

The JSONArray is dynamic, so sometimes it is the 73rd, 74th, or 75th object in the array and none of the objects in the array have names. I am currently using this code to parse it. It works fine when my particular object is in the 75th position, but crashes when it is not.

try {
         JSONArray json = JSONfunctions2.getJSONfromURL("http://bitcoincharts.com/t/markets.json");
         JSONObject  forex = json.getJSONObject(75);
         String btc = forex.getString("avg");            
         currencyBTC = Double.parseDouble(btc);  
 }catch(JSONException e)        {
     Log.e("log_tag", "Error parsing data "+e.toString());
 }       

is it possible for me to identify the object by it's attributes, since the objects in the array have no names? How can i resolve this issue? Thank you in advance.


Edit:

This somewhat works, but only returns the values from the last object in the array. How do I handle this so that i can parse my particular object, and not just the last one? ...

 try {
JSONArray jArray = JSONfunctions2.getJSONfromURL("http://bitcoincharts.com/t/markets.json");
String symbol = "mtgoxUSD";
for (int i = 0; i < jArray.length(); i++) {
    JSONObject forex = jArray.getJSONObject(i);
    String mtgoxUSD = forex.getString("symbol");
    if (mtgoxUSD == symbol) {
        String btc = forex.getString("avg");            
        double currencyBTC = Double.parseDouble(btc);
    }
}
} catch (Exception e) {
Log.e("log_tag", "Error parsing data "+ e.toString());
}
B. Money
  • 931
  • 2
  • 19
  • 56
  • Break out the `"mtgoxUSD"` if the `if` statement to a `String` like I have it. I believe that's where it's going wrong. But looking at it, I can't see how it would give the results you described... – jnthnjns Jul 26 '12 at 02:24
  • i was thinking maybe i could somehow parse everything into an ArrayList and then get it from the ArrayList – B. Money Jul 26 '12 at 03:00
  • Sorry, I read your last comment wrong, of course you could parse into an `ArrayList` just set up the Array outside of the `for` loop then use `add` to add values to the List inside. I thought you were only wanting the one value. You original question was answered. – jnthnjns Jul 26 '12 at 12:24
  • I do only want the one value. is it possible to parse my arraylist for the particular value i want? here is my code... JSONArray json = JSONfunctions2.getJSONfromURL("http://bitcoincharts.com/t/markets.json"); resultList = new ArrayList(json.length()); for (int i = 0; i < json.length(); i++) { resultList.add(json.getJSONObject(i).getString("symbol").); resultList.add(json.getJSONObject(i).getString("avg")); – B. Money Jul 31 '12 at 02:14

2 Answers2

1

This the way that I parse the JSON in an android application :

String s = client.getBaseURI("http://bitcoincharts.com/t/markets.json"); // Json format
JSONArray array = new JSONArray(s);
JSONObject obj;     
for (int i = 0; i < array.length(); i++) {
     obj = (JSONObject) array.get(i);
     double average =Double.parsedouble(obj.get("avg").toString()));
     String symbol = obj.get("symbol").toString();
     double low = Double.parsedouble(obj.get("low").toString());
}

I also want to add that I use HTTP Client library to fetch the data from server. To have more information about how to use HTTP Client, check my answer in this link: HTTP Client

Community
  • 1
  • 1
Ali
  • 9,800
  • 19
  • 72
  • 152
  • This requires that every JSONObject contains a `avg` string. – nuala Jul 25 '12 at 20:54
  • 1
    @Ali - If you are developing for Gingerbread or "better" you should change from `HTTPClient` to `HttpURLConnection`. From Android Dev-Blog: "For Gingerbread and better, HttpURLConnection is the best choice. Its simple API and small size makes it great fit for Android. Transparent compression and response caching reduce network use, improve speed and save battery. New applications should use HttpURLConnection; it is where we will be spending our energy going forward." [Source](http://android-developers.blogspot.com/2011/09/androids-http-clients.html) – jnthnjns Jul 25 '12 at 21:32
0

Is the "75" going to be dynamic as well? Meaning, will the number changed based on user input? If so, you'll need to have a handle for that but anyway, just use a for loop, something like the following:

try {
    JSONArray jArray = JSONfunctions2.getJSONfromURL("http://bitcoincharts.com/t/markets.json");
    String symbol = "mtgoxUSD";
    for (int i = 0; i < jArray.length(); i++) {
        JSONObject forex = jArray.getJSONObject(i);
        String mtgoxUSD = forex.getString("symbol");
        if (mtgoxUSD == symbol) {
            String btc = forex.getString("avg");            
            double currencyBTC = Double.parseDouble(btc);
        }
    }
} catch (Exception e) {
    Log.e("log_tag", "Error parsing data "+ e.toString());
}
jnthnjns
  • 8,962
  • 4
  • 42
  • 65
  • The "75" doesn't change based on user input. The object I intend to get is in the 75th position in this case, but it's position changes randomly in the array i want to parse. If it helps, the object that i always want contains the string "symbol":mtgoxUSD. – B. Money Jul 25 '12 at 20:51
  • If it always contains that string then just search through the array for that string – jnthnjns Jul 25 '12 at 20:53
  • i appreciate you edit. It's not parsing correctly though. It loops through the entire array and sets String mtgoxUSD equal to "symbol" in the last object, which is ruxumZAR. Is there a way to stop the loop at my desired object? – B. Money Jul 26 '12 at 00:27
  • Edit your question to show your new code. Sounds like you missed an `=`. When comparing, 'if equal to', always use `==` – jnthnjns Jul 26 '12 at 01:08