如何从多个Json数组中获取值(如下所示)在Android中?

时间:2018-10-30 07:07:37

标签: java android arrays 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"
   ]
}

我需要从上面的Json数组中获取值。 Json数组的名称,例如:Willian Engineering Pte Ltd,都是包含在arraylist中的所有动态值。我的意思是说Json数组的名称存储在arraylist中。

有人可以告诉我如何获取所有值吗?

谢谢

4 个答案:

答案 0 :(得分:0)

您可以这样:

    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();
    }

答案 1 :(得分:0)

您可以遍历JSONObject中的所有键。喜欢,

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

然后您可以从此allKeyList获取所有 JSONArray

答案 2 :(得分:-1)

这似乎是一个非常普遍的问题,但是您这里有一个大object,其中有member个。每个member是一个array。因此,您可能应该遍历对象的member,然后对于每个member array遍历对象。 https://www.json.org/

编辑:这可能是一个很好的起点https://stackoverflow.com/a/13573965/568735

编辑:我想成员应该转换为JSONArray个。

答案 3 :(得分:-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));;
        }
    }

使用JSONObject软件包中的org.json