0
{
   "stocks":{
      "0":{
         "name":"Torn City Stock Exchange",
         "acronym":"TCSE",
         "director":"None",
         "current_price":10018.747,
         "market_cap":0,
         "total_shares":0,
         "available_shares":0,
         "forecast":"Average",
         "demand":"High"
      },
      "1":{
         "name":"Torn City and Shanghai Banking Corporation",
         "acronym":"TSBC",
         "director":"Mr. Gareth Davies",
         "current_price":529.863,
         "market_cap":4300246833486,
         "total_shares":8115771121,
         "available_shares":0,
         "forecast":"Average",
         "demand":"High",
         "benefit":{
            "requirement":4000000,
            "description":"Entitled to receive occasional dividends"
         }
      },
      "2":{
         "name":"Torn City Investment Banking",
         "acronym":"TCB",
         "director":"Mr. Paul Davies",
         "current_price":502.819,
         "market_cap":5771083717274,
         "total_shares":11477457529,
         "available_shares":1539811799,
         "forecast":"Average",
         "demand":"Average",
         "benefit":{
            "requirement":1500000,
            "description":"Entitled to receive improved interest rates"
         }
      }
}

How can I loop through all the items with key-value pairs? I want to extract name, acronym , total shares, available_shares and demand. Please help! I'm creating a android app using java and I'm stuck here.

2
  • 1
    Can't you get a JSON array for 'stocks' from the server side / source? That would make your life a lot easier iterating through it. Commented Aug 21, 2020 at 12:28
  • Letme try it @SiddharthKamaria Commented Aug 21, 2020 at 12:29

2 Answers 2

5

IMO this is not a proper way of having a list of objects in a JSON. If you can, you should change this to

{
  "stocks": [
    {
      "name": "Torn City Stock Exchange",
      "acronym": "TCSE",
      "director": "None",
      "current_price": 10018.747,
      "market_cap": 0,
      "total_shares": 0,
      "available_shares": 0,
      "forecast": "Average",
      "demand": "High"
    },
    {
      "name": "Torn City and Shanghai Banking Corporation",
      "acronym": "TSBC",
      "director": "Mr. Gareth Davies",
      "current_price": 529.863,
      "market_cap": 4300246833486,
      "total_shares": 8115771121,
      "available_shares": 0,
      "forecast": "Average",
      "demand": "High",
      "benefit": {
        "requirement": 4000000,
        "description": "Entitled to receive occasional dividends"
      }
    },
    {
      "name": "Torn City Investment Banking",
      "acronym": "TCB",
      "director": "Mr. Paul Davies",
      "current_price": 502.819,
      "market_cap": 5771083717274,
      "total_shares": 11477457529,
      "available_shares": 1539811799,
      "forecast": "Average",
      "demand": "Average",
      "benefit": {
        "requirement": 1500000,
        "description": "Entitled to receive improved interest rates"
      }
    }
  ]
}

An ideal way would be to have a mapping class in Java and capture the JSON as List, then you can easily iterate.

private String json = "your json object here";

ObjectMapper objectMapper = new ObjectMapper();

List<MappingClass> stocks = objectMapper.readValue(jsonArray, new 
                            TypeReference<List<MappingClass>>(){});

  for (MappingClass stock : stocks) {
      stock.getName();
      // other fields
  }

Try having a look over The Jackson Library and ObjectMapper for reference.

Sign up to request clarification or add additional context in comments.

Comments

2

You will probably want to use a well known JSON library such as GSON or JsonPullParser. GSON can deserialize your JSON into a Map that contains objects that you will have to define. So it will be something like a Map<Integer, Bank>. Your Bank class would look something like this:

public class Bank {
   String acronym;
   String name;
   //...
   Benefit benefit;
}

and then, if you use GSON, your parsing code will look something like this:

Map<Integer,Bank> bankMap;

Gson gson = new Gson();

Type type = TypeToken.getParameterized(Map.class, Integer.class, Bank.class).getType();
//result is a String that has your JSON!
bankMap = gson.fromJson(result, type);
for (Bank bank : bankMap.values()) {
   System.out.println("Bank name:" + bank.name);
   //...
}

The documentation for GSON is comprehensive and well written. It can be found here: https://github.com/google/gson

I hope what I wrote helps you! Good luck and please let me know how things went.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.