将JSONArray转换为List

时间:2018-04-04 21:04:52

标签: java json

我在休息服务调用中返回了JSONArray,如下所示:

frequencies3 <- frequencies2 %>%
  mutate(pvalue = map_dbl(M, ~chisq.test(.x)$p.value)) %>%
  select(-data, -M) %>%
  ungroup()
frequencies3
# # A tibble: 4 x 3
#   contig_ID         ecotype pvalue
#   <fct>             <fct>    <dbl>
# 1 Contig100169_2367 Crab     1.00 
# 2 Contig100169_2367 Wave     0.434
# 3 Contig100169_2481 Crab     0.284
# 4 Contig100169_2481 Wave     0.958

我需要将其转换为

[
 {"REFDATA_KEY":"Rf1","REFDATA_VALUE":"Daily"},
 {"REFDATA_KEY":"Rf2","REFDATA_VALUE":"Weekly"}, 
 {"REFDATA_KEY":"Rf3","REFDATA_VALUE":"Monthly"}
]

如何转换它?

2 个答案:

答案 0 :(得分:1)

你可以很简单。

map()

或者您可以使用var sd = [{"REFDATA_KEY":"Rf1","REFDATA_VALUE":"Daily"},{"REFDATA_KEY":"Rf2","REFDATA_VALUE":"Weekly"},{"REFDATA_KEY":"Rf3","REFDATA_VALUE":"Monthly"}] var list = sd.map(obj =>{ var returned = {}; returned['Value'] = obj['REFDATA_KEY']; returned['Label'] = obj['REFDATA_VALUE']; return returned; }) console.log(list);函数:

    shaded.com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden 
    { 
      "code" : 403,
      "errors" : [ {  
      "domain" : "global",  
      "message" : "Request had insufficient authentication scopes.", 
      "reason" : "forbidden"  
      } ], 
     "message" : "Request had insufficient authentication scopes.", 
     "status" : "PERMISSION_DENIED" 
    }  
at shaded.com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:146) 
at shaded.com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113) 
at shaded.com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40) 
at shaded.com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:321) 
at shaded.com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1049) 
at shaded.com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419) 
at shaded.com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352) 
at shaded.com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)

答案 1 :(得分:1)

您可以使用org.json库(https://mvnrepository.com/artifact/org.json/json)来实现以下目标:

我们假设您在response JSONArray变量中收到了回复。

JSONArray response = [
 {"REFDATA_KEY":"Rf1","REFDATA_VALUE":"Daily"},
 {"REFDATA_KEY":"Rf2","REFDATA_VALUE":"Weekly"}, 
 {"REFDATA_KEY":"Rf3","REFDATA_VALUE":"Monthly"}
]

您可以编写reformJsonArray()函数,如下所示,response并根据需要返回List<JSONObject>

public List<JSONObject> test(JSONArray json) {
    List<JSONObject> jsonArrayList = new ArrayList<>();
    for (int index = 0; index < json.length(); index++) {
        JSONObject element = json.getJSONObject(index);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("value", element.getString("REFDATA_KEY")).put("label", element.getString("REFDATA_VALUE"));

        jsonArrayList.add(jsonObject);
    }
    return jsonArrayList;
}