0

我从一个网站通过HttpClient.

[
    {
        "scout": "webdriver",
        "short_version": "4.3",
        "long_name": "iPad",
        "api_name": "ipad",
        "long_version": "4.3.",
        "automation_backend": "webdriver",
        "os": "Mac 10.4"
    },
    {
        "scout": "webdriver",
        "short_version": "5.0",
        "long_name": "Firefox",
        "api_name": "firefox",
        "long_version": "14",
        "automation_backend": "webdriver",
        "os": "Mac 10.5"
    },
    {
        "scout": "webdriver",
        "short_version": "4",
        "long_name": "Firefox",
        "api_name": "firefox",
        "long_version": "15",
        "automation_backend": "webdriver",
        "os": "Mac 10.6"
    },
    {
        "scout": "webdriver",
        "short_version": "5",
        "long_name": "Chrome",
        "api_name": "chrome",
        "long_version": "5.0.1.",
        "automation_backend": "webdriver",
        "os": "Mac 10.7"
    }
]

我想把这些数据变成这种格式:

[OS: {Mac10.4,Mac10.5}]

等,操作系统列表

[{api_name: {firefox:{14,15}} }]

等,浏览器列表及其版本。

操作系统和浏览器列表太长。如何使用 json 或任何其他方法解析它?

4

2 回答 2

1

我建议你看看 json-smart 库。它重量轻,速度快。(https://code.google.com/p/json-smart/

然后你可以写这样的东西(只是例子):

    Map<String, List<String>> osMap = new HashMap<String, List<String>>();
    osMap.put("OS", new ArrayList<String>());

    Map<String, Map<String, List<String>>> apiNameMap = 
        new HashMap<String, Map<String, List<String>>>();
    apiNameMap.put("api_name", new HashMap<String, List<String>>());

    JSONArray ja = (JSONArray) JSONValue.parse(JSON_STRING);
    for (Object obj : ja) {
        JSONObject jsonObj = (JSONObject) obj;
        String os = (String) jsonObj.get("os");
        String apiName = (String) jsonObj.get("api_name");
        String longVersion = (String) jsonObj.get("long_version");

        // OS
        if (!osMap.get("OS").contains(os)) {
            osMap.get("OS").add(os);
        }

        // API
        if (!apiNameMap.get("api_name").containsKey(apiName)) {
            apiNameMap.get("api_name")
                    .put(apiName, new ArrayList<String>());
        }
        apiNameMap.get("api_name").get(apiName).add(longVersion);

    }

    System.out.println(JSONValue.toJSONString(osMap));
    System.out.println(JSONValue.toJSONString(apiNameMap));
于 2013-09-18T12:47:06.747 回答
0

这样做:

$(document).ready(function(){
//here getting the json Array from session
    var d  = json1;
    //after making a variable array to put j son data one by one by looping
    var availableTags = new Array();
    for(var i=0;i<d.forms.length;i++){
        availableTags[i] = d.forms[i].productName;
    }
});
于 2013-09-18T12:28:17.547 回答