JSONObject的使用二:jsonObject中对于JSONArray的使用

public String getProductFamilyNodes(String aJsonStr)
        throws ApplicationException
    {
        JSONObject json;
        String itemId = "";
        String nlsLang = "";
        try
        {
            json = new JSONObject(aJsonStr);
            itemId = json.getString("itemId");
            nlsLang = json.getString("nlsLang");
        }
        catch (JSONException e)
        {
            e.printStackTrace();
        }
        List<Map<String, Object>> familyNodes = iNavigationSev.getProductFamilyNodes(itemId,nlsLang);
        return JsonUtil.transferStringByList(familyNodes);
    }

    public static String transferStringByList(List<Map<String, Object>> list)
    {
        String jsonStr = "";
        JSONArray jsonArray = new JSONArray();
        try
        {
            for (Map<String, Object> tmpMap : list)
            {
                jsonArray.put(tmpMap);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return jsonArray.toString();
    }
@Test
    public void testGetProductFamilyNodes() throws Exception
    {
        JSONObject json = new JSONObject();
        json.put("itemId", "29-2");
        json.put("nlsLang", "zh");
        String jsonStr = json.toString();
        JSONArray jsonArr = new JSONArray(
                iNavigationWs.getProductFamilyNodes(jsonStr));
        List listNodes = new ArrayList();
        for(int i=0;i<jsonArr.length();i++){
            Map map = new HashMap();
            JSONObject jsonTest = jsonArr.getJSONObject(i);
            System.out.println("========json"+i+": " + jsonTest.getString("ITEM_NAME")); 
            map.put("ITEM_NAME", jsonTest.getString("ITEM_NAME"));
            listNodes.add(map);
        }
}

你可能感兴趣的:(json)