我通过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}]
等,OS列表
和
[{api_name: {firefox:{14,15}} }]
等,浏览器列表及其版本。
列表太长了OS和浏览器。 如何使用json或任何其他方法解析它?
答案 0 :(得分: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));
答案 1 :(得分: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;
}
});