1

I know the answer for parsing the JSON of this type:

                    { "id": "1001", "type": "Regular" },
                    { "id": "1002", "type": "Chocolate" },
                    { "id": "1003", "type": "Blueberry" },
                    { "id": "1004", "type": "Devil's Food"}

where there is key value pair, with key being same(like 'id' here) and value differing, and we use a for loop to parse it quickly.

(For those who'd like to see how to parse above JSON, please go to this link: How to parse nested JSON object using the json library?)

However, the JSON I'm trying to parse is a different one which doesn't have a same key like 'Id' as above for every different value, but every key is a new key with a different value. Below is the example:

{
  "disclaimer": "Exchange rates are ...........blah blah",
  "license": "Data sourced from various .......blah blah",
  "timestamp": 1446886811,
  "base": "USD",
  "rates": {
    "AED": 3.67266,
    "AFN": 65.059999,
    "ALL": 127.896
.
.
All the currency values.
.
   }
}

I'm not sure how to parse the above one with all different keys of currencies (currency like AED and their value) and pop them up in a drop down list.

Do I have to write a new line of code for each different currency and value pair or it is in some way possible to use a for loop for this one as well.

Can someone provide some lines code if possible?

0

2 Answers 2

2

you can use org.json for this thing.

E.g.:

JSONObject json = new JSONObject("<jsonString>");
 Iterator<String> keys = json.keys();

    while (keys.hasNext()) {
        String key = keys.next();
        System.out.println("Key :" + key + "  Value :" + json.get(key));
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so very much for the help and time! If you don't mind, could you please also mention how to pop up the parsed results into a drop down list on Android. This will help the other visitors of this page and me as well. My question is answered and it helped, I tried to vote 1 up, but as I don't have a '15' reputation(new here), it's not getting counted.
Please look into this for more help : androidbegin.com/tutorial/…
1

You could use GSON in this case. I will just print the currencies with the according rate but you can build a different data structure(a map for example) and use it in your system.

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.io.IOException;
import java.util.Map;

public class Main {

    public static void main(String[] args) throws IOException {
        String jsonString = "{\n" +
                "  \"disclaimer\": \"Exchange rates are ...........blah blah\",\n" +
                "  \"license\": \"Data sourced from various .......blah blah\",\n" +
                "  \"timestamp\": 1446886811,\n" +
                "  \"base\": \"USD\",\n" +
                "  \"rates\": {\n" +
                "    \"AED\": 3.67266,\n" +
                "    \"AFN\": 65.059999,\n" +
                "    \"ALL\": 127.896\n" +
                "  }\n" +
                "}";
        JsonObject jsonObject = new JsonParser().parse(jsonString).getAsJsonObject();
        for(Map.Entry<String, JsonElement> currency: jsonObject.getAsJsonObject("rates").entrySet()){
            System.out.println("Currency "+ currency.getKey()+" has rate " + currency.getValue());
        }
    }
}

1 Comment

Thank you very much for your help and time, I tried to vote 1 up for your answer, but as I don't have 15 reputation, it's not getting counted. :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.