0
{  
   "EMERSON PARTS PTE LTD":[  
      "7832.4000000000000"
   ],
   "DATASELL CO LTD":[  
      "6099.0000000000000"
   ],
   "CATERMAS AUTO PARTS PTE LTD":[  
      "4633.1000000000000"
   ],
   "CONNOR TRADING LTD":[  
      "2166.7500000000000",
      "1402.7700000000000"
   ],
   "Telemart Paper (M) Sdn.Bhd.":[  
      "954.4400000000000",
      "19088.8000000000000"
   ],
   "ASTRO ASIAN PTE LTD":[  
      "22598.4000000000000"
   ],
   "ABC Pte Ltd":[  
      "535.0000000000000",
      "535.0000000000000"
   ],
   "AMERICAN COMPUTER SUPPLY CO PTE LTD":[  
      "321.0000000000000"
   ],
   "Willian Engineering Pte Ltd":[  
      "1408.6300000000000",
      "1970.7700000000000",
      "414.3000000000000",
      "548.9500000000000"
   ]
}

I need to fetch values from the above Json arrays. The name of the Json array for example: Willian Engineering Pte Ltd, are all dynamic values containing in a arraylist. I mean to say the names of the Json array are stored in an arraylist.

Can someone tell me how to fetch the entire values?

Thanks

Suneel Kumar
  • 5,621
  • 3
  • 31
  • 44

4 Answers4

1

You can traverse with all keys from JSONObject. Like,

JSONObject obj = new JSONObject(string);
    List<String> allKeyList = new ArrayList<String>();
    Iterator<String> keys = obj.keys();
    while (keys.hasNext()) {
        allKeyList.add(keys.next());
    }

Then you can get all JSONArray from this allKeyList.

Abhay Koradiya
  • 2,068
  • 2
  • 15
  • 40
0

You could do like this:

    String originStr="{\"EMERSON PARTS PTE LTD\":[\"7832.4000000000000\"],\"DATASELL CO LTD\":[\"6099.0000000000000\"],\"CATERMAS AUTO PARTS PTE LTD\":[\"4633.1000000000000\"],\"CONNOR TRADING LTD\":[\"2166.7500000000000\",\"1402.7700000000000\"],\"Telemart Paper (M) Sdn.Bhd.\":[\"954.4400000000000\",\"19088.8000000000000\"],\"ASTRO ASIAN PTE LTD\":[\"22598.4000000000000\"],\"ABC Pte Ltd\":[\"535.0000000000000\",\"535.0000000000000\"],\"AMERICAN COMPUTER SUPPLY CO PTE LTD\":[\"321.0000000000000\"],\"Willian Engineering Pte Ltd\":[\"1408.6300000000000\",\"1970.7700000000000\",\"414.3000000000000\",\"548.9500000000000\"]}";
    String searchStr; //for example: "Willian Engineering Pte Ltd"
    JSONObject originJsonObj;
    JSONArray resultJsonArray;
    String resultStr;


    try {
          originJsonObj = new JSONObject(originStr);
          resultJsonArray = originJsonObj.getJSONArray(searchStr);
          resultStr = resultJsonArray.toString();
    } catch (JSONException e) {
          e.printStackTrace();
    }
navylover
  • 12,383
  • 5
  • 28
  • 41
  • Sorry but searching is not allowed. I already mentioned that the name of Json is coming from an arraylist. –  Oct 30 '18 at 07:40
  • @GargiBose You could extract my codes to a function and loop the arraylist. – navylover Oct 30 '18 at 07:49
  • ok but can you tell how to sum up the values ? I mean for values containing multiple against the key? –  Oct 30 '18 at 09:31
-1

This seems like a pretty general question, but what you have here is one big object, which has members. Each member is an array. So you should probably iterate trough the members of your object, and then for each member array iterate trough it. https://www.json.org/

EDIT: This might be a good starting point https://stackoverflow.com/a/13573965/568735

EDIT: I guess the members should be converted to JSONArrays.

Emil Vatai
  • 2,298
  • 1
  • 18
  • 16
  • "probably", "might be", "I guess" ? and referencing another answer should be a comment, not an answer – Sharon Ben Asher Oct 30 '18 at 07:28
  • The explanation that he has an object, not just "arrays" doesn't qualify? (What is the proper procedure in this case, should I delet my answer and put it as a comment?) – Emil Vatai Oct 30 '18 at 07:30
  • if you think this is a duplicate of another question you should vote to close it with reference to the duplicate. otherwise delete this answer and add a comment – Sharon Ben Asher Oct 30 '18 at 07:32
  • But... if OP simply doesn't realizes that he has a single big object in the mix? As far as I can tell, that was the pivotal point he/she was missing... (That's why I wrote it as an answer, referencing json specification) Referencing the other answer was sort of a "get things started" link. – Emil Vatai Oct 30 '18 at 07:35
  • perhaps we need OP to clarify what he/she realise and what not? – Sharon Ben Asher Oct 30 '18 at 07:36
  • @SharonBenAsher I am sorry but this is not a duplicate as I have already made a search before posting my question. –  Oct 30 '18 at 07:38
  • @EmilVatai I know the json has a big json object and thats the reason i asked for a solution. –  Oct 30 '18 at 07:39
  • @GargiBose then perhaps you can clarify what is the expected output and perhaps some more surrounding code like the array of names and how you want to process it – Sharon Ben Asher Oct 30 '18 at 07:39
-1
public static void main(String[] args) {

        String json="{\"EMERSON PARTS PTE LTD\":[\"7832.4000000000000\"],\"DATASELL CO LTD\":[\"6099.0000000000000\"],\"CATERMAS AUTO PARTS PTE LTD\":[\"4633.1000000000000\"],\"CONNOR TRADING LTD\":[\"2166.7500000000000\",\"1402.7700000000000\"],\"Telemart Paper (M) Sdn.Bhd.\":[\"954.4400000000000\",\"19088.8000000000000\"],\"ASTRO ASIAN PTE LTD\":[\"22598.4000000000000\"],\"ABC Pte Ltd\":[\"535.0000000000000\",\"535.0000000000000\"],\"AMERICAN COMPUTER SUPPLY CO PTE LTD\":[\"321.0000000000000\"],\"Willian Engineering Pte Ltd\":[\"1408.6300000000000\",\"1970.7700000000000\",\"414.3000000000000\",\"548.9500000000000\"]}";        
        JSONObject jsonObject = new JSONObject(json);           
        Set<String> keys =jsonObject.keySet();
        for(String key:keys) {
            System.out.println("Key :: "+key +", Value :: "+jsonObject.get(key));;
        }
    }

Use the JSONObject from org.json Package