0

In my java project i got the currency code and its expansion as a json string as a result of an api call.Below is the json result.

      String jsonString = {
    "AED": "United Arab Emirates Dirham",
    "AFN": "Afghan Afghani",
    "ALL": "Albanian Lek",
    "AMD": "Armenian Dram",
    "ANG": "Netherlands Antillean Guilder",
    "AOA": "Angolan Kwanza",
    "ARS": "Argentine Peso",
    "AUD": "Australian Dollar",
    "AWG": "Aruban Florin",
    "AZN": "Azerbaijani Manat",
    "BAM": "Bosnia-Herzegovina Convertible Mark",
    "BBD": "Barbadian Dollar",
    "BDT": "Bangladeshi Taka",
    "BGN": "Bulgarian Lev",
    "BHD": "Bahraini Dinar",
    "BIF": "Burundian Franc",
    "BMD": "Bermudan Dollar",
    "BND": "Brunei Dollar",
    "BOB": "Bolivian Boliviano",
    "BRL": "Brazilian Real",
    "BSD": "Bahamian Dollar",
    "BTC": "Bitcoin",
    "BTN": "Bhutanese Ngultrum",
    ......
    }

Now how can i extract the key value pair from above json string where the currency code comes as the key and expansion comes as the value, so that i can store it to a list or map.

KJEjava48
  • 1,967
  • 7
  • 40
  • 69
  • Wait! In Java we have class Currency which represents a currency. Currencies are identified by their ISO 4217 currency codes. Give me a reason for not using it before going further. – xenteros Nov 10 '16 at 13:00
  • 3
    See this Stackoverflow response: https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java. – boky Nov 10 '16 at 13:02
  • More relevant duplicate. http://stackoverflow.com/questions/443499/convert-json-to-map – OneCricketeer Nov 10 '16 at 13:21
  • @cricket_007 I think the structure of my json is different from that – KJEjava48 Nov 10 '16 at 13:41
  • @boky I think the structure of my json is different from that – KJEjava48 Nov 10 '16 at 13:41
  • Did you even try it? JSON is key value pairs. Doesn't matter what format you have – OneCricketeer Nov 10 '16 at 13:44
  • @cricket_007 which library i should import for ObjectMapper() ? fasterxml or codehaus ? – KJEjava48 Nov 11 '16 at 05:05
  • One was renamed to the other. I cannot remember which – OneCricketeer Nov 11 '16 at 05:07
  • `com.fasterxml.jackson.databind` is the latest version. If you see both, then you are using mixed versions of the library – OneCricketeer Nov 11 '16 at 05:09
  • @cricket_007 Is there any other way to loop this key value pair, when we iterate this hash map it will not give the correct order.Its not necessary to put the key-value pair to a hashmap – KJEjava48 Nov 11 '16 at 05:53
  • TreeMap should be ordered – OneCricketeer Nov 11 '16 at 06:04
  • @cricket_007 but how can i map this json String to treemap – KJEjava48 Nov 11 '16 at 06:07
  • I don't really know what you're wanting to get. Sorry. There's like 5 json libraries that I know. You seem to have picked Jackson, so like, you need to iterate the JSON tree http://stackoverflow.com/questions/19760138/parsing-json-in-java-without-knowing-json-format – OneCricketeer Nov 11 '16 at 06:11
  • @cricket_007 I just want to map the json string in its alphabetical order as in the question.If i map them to hashmap, then when i iterate that map i getting the re result in unordered manner. – KJEjava48 Nov 11 '16 at 06:20

1 Answers1

0

You can use jackson for reading json from Inputstream/String/byte[]/Url/File/Reader to Map<String,String> using below code:

  ObjectMapper mapper = new ObjectMapper();// do this construction at application level. Not for every call.
  Map<String,String> codeDescMap = mapper.readValue(source,mapper.getTypeFactory()
        .constructMapLikeType(Map.class,
              String.class,String.class));

where source can be Inputstream/String/byte[]/Url/File/Reader with valid json structure.

Shashank
  • 416
  • 5
  • 16
  • This doesn't answer how to get any data from the JSON. And Jackson seems like it is unnecessary – OneCricketeer Nov 10 '16 at 13:15
  • Use case of op is to read data from api call and store it in map or list.Since it is api call he will any how needs to deserialize data. – Shashank Nov 10 '16 at 13:19