将JSON数组分配给另一个JSON数组变量

时间:2018-04-05 07:13:23

标签: java arrays json

我有一个像这样的JSON数组:

[  
   [  
      {  
         "label":"Red",
         "value":"8"
      },
      {  
         "label":"Yellow",
         "value":"9"
      },
      {  
         "label":"Pink",
         "value":"10"
      }
   ]
]

我需要将此值赋给新的JSON Array变量,并且应该解析它。 Java有没有办法实现这个目标?

帮助很有用!

2 个答案:

答案 0 :(得分:0)

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Test {

    public static void main(String[] args) throws Exception {

        ObjectMapper mapper = new ObjectMapper();

        String jsonInString = "[ [ { \"label\":\"Red\", \"value\":\"8\" }, { \"label\":\"Yellow\", \"value\":\"9\" }, { \"label\":\"Pink\", \"value\":\"10\" } ] ]";
        // Convert JSON string to Object
        JsonNode node = mapper.readValue(jsonInString, JsonNode.class);
        if (node.isArray()) {
            // your parsing logic
        }

    }

}

参考:https://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/

答案 1 :(得分:-1)

org.json.simple.JSONArray yourArray = new org.json.simple.JSONArray();
org.json.JSONArray arr = new org.json.JSONArray(yourArray);