Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3217544
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:25:24+00:00 2026-05-17T15:25:24+00:00

Before I begin, this is my JSON: (The structure is rigid, and cant be

  • 0

Before I begin, this is my JSON: (The structure is rigid, and cant be changed)

[
  {"_id":{"country":"au", "industry":"foo"}, "value":{"count":2}},
  {"_id":{"country":"au", "industry":"bar"}, "value":{"count":1}},
  {"_id":{"country":"be", "industry":"baz"}, "value":{"count":2}},
  ..
]

I cant have duplicate country names, and industry names. I have an array that is to be filled with values as

array[au][foo] = 2

array[au][bar] = 1

array[be][baz] = 2

the values are unsorted in the JSON, and all countries might not have the same industries.

How do i go about doing that? This is my current code:

for (int i = 0; i < jsonArray.size(); i++) {
        JSONObject jsonValue = jsonArray.get(i).isObject();

        JSONObject _id = jsonValue.get("_id").isObject();
        JSONObject value = jsonValue.get("value").isObject();

        String country = _id.get("country").isString().toString();
        setCountry.add(country);

        String industry = _id.get("industry").isString().toString();
        setIndustry.add(industry);

        int count = Integer.parseInt(value.get("count").isNumber()
                .toString());

    }

Im adding the country and industry to a set, to remove the duplicates. thats what causing the issue about the count. I dont care for it to be elegant, a hackjob will also do.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T15:25:25+00:00Added an answer on May 17, 2026 at 3:25 pm

    I think you can make use of enum’s for your problem.Define all the known country names and Industry in an enum like this.

    public enum Country {
    au,
    be;
    int Int=this.ordinal();//just a short name for ordinal
    }
    

    and

    public enum Industry {
    foo,
    bar,
    baz;
    int Int=this.ordinal();
    }
    

    Now define a 2d int array and you can set the value’s using the enum’s like this :

    int[][] value=new int[Country.values().length][Industry.values().length];
    value[Country.au.Int][Industry.bar.Int]=2;
    //Read from JSON
    value[Country.valueOf("au").Int][Industry.valueOf("bar").Int]=2;
    

    You can add this code to the end of your current for loop if your using enum’s:

    value[Country.valueOf(country).Int][Industry.valueOf(industry).Int]=count;
    

    Another option is to avoid an array and use Map instead:

    Map<Country,Map<Industry,Integer>> m=new HashMap<Country,Map<Industry,Integer>>();
    

    or simply without enums :

    Map<String,Map<String,Integer>> m=new HashMap<String,Map<String,Integer>>();
    

    The problem with map is that it is a bit tricky to add and retrieve value’s from it but you can write common method’s to do this job.

    UPDATE:

    Adding values to inner map:

    String[][] countryAndIndustry= {{"au","foo"},{"au","bar"},{"be","baz"}};
    Integer[] count= {2,1,2};
    HashMap<String,HashMap<String,Integer>> hm=new HashMap<String,    HashMap<String,Integer>>();
    for(int i=0;i<count.length;i++)
    {
        HashMap<String,Integer> innerMap=hm.get(countryAndIndustry[i][0]);
        if(innerMap==null)
        {
            innerMap=new HashMap<String, Integer>();
            hm.put(countryAndIndustry[i][0],innerMap);
        }
        innerMap.put(countryAndIndustry[i][1],count[i]);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Before I begin, I would like to highlight the structure of what I am
Before I begin looking into this myself, I wanted to check if anyone had
Before you answer this I have never developed anything popular enough to attain high
Before I start, I know there is this post and it doesn't answer my
Before I do this I figured I would ask if it was the best
Before anyone suggests scrapping the table tags altogether, I'm just modifying this part of
Before you start firing at me, I'm NOT looking to do this, but someone
Before you answer, this question is complicated: We are developing in asp.net / asp.net
I know this issue has been touched on before, e.g. here: How to post
Before I begin, my reason for not using OAuth is I believe it is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.