1

I have a JSON file that has stock data of different portfolios.

[
    {
        "name": "octoberportfolio", 
        "pathame": "C:\\Users\\Documents\\octoberportfolio.xlsx", 
        "stocks" :
             {
                "SWN": ["Southwestern Energy", 34, 23],
                "TSLA": ["Tesla", 4, 2100],
                "HPE": ["Hewlett Packard Enterprise", 9, 400]
            }
    },

    {
        "name": "jasonportfolio", 
        "pathame": "C:\\Users\\Documents\\Java\\jasonportfolio.xlsx", 
        "stocks" :
             {
                "AAPL": ["Apple", 34, 23],
                "MSFT": ["Microsoft", 4, 2100],
                "BAB": ["Alibaba", 9, 400]
            }

    }
]

I'd like to add the key values for "name" to an ArrayList ["octoberportfolio", "jasonportfolio"]. How would I do this? The only solution I've seen online is if there is only one object in the file.
Thanks

  • 2
    It looks like the `stocks` would be a Map – Scary Wombat Oct 15 '21 at 02:36
  • Further, it looks like the stock object, eg `["Apple", 34, 23]` should not be an array of untyped objects, ie `Object[]`, but should be an object, eg `{"name":"Apple", "num1" : 34, "num2" : 23}` – Bohemian Oct 15 '21 at 02:42
  • @Bohemian The reason I wrote it like that is because I needed to have multiple attributes for multiple stocks, and not just one stock – Wilhelmo Gutred Oct 15 '21 at 02:44
  • @WilhelmoGutred what I mean is `"stocks": { "APPL": {"name":"Apple", "num1" : 34, "num2" : 23}, "MSFT": {"name":"Microsoft", "num1" : 4, "num2" : 2100} }` – Bohemian Oct 15 '21 at 02:46
  • @Bohemian oh ok I understand now, I was just wondering how you would get the values for the "name" key for both portfolios – Wilhelmo Gutred Oct 15 '21 at 02:50
  • Use jackson's ObjectMapper and deserialize into a class that includes a field `Map stocks;` and go from there. – Bohemian Oct 15 '21 at 04:52

0 Answers0