【问题标题】:Assigning JSON Array to another JSON Array variable将 JSON 数组分配给另一个 JSON 数组变量
【发布时间】:2018-04-05 07:13:22
【问题描述】:

我有一个这样的 JSON 数组:

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

我需要将此值分配给新的 JSON 数组变量,并且应该解析相同的值。 Java有什么办法可以做到这一点吗?

帮助很有用!

【问题讨论】:

  • 我正在与simple JSON合作
  • 你有一个 Json 字符串,需要转换成 JsonArray?试试fasterxml.github.io/jackson-databind
  • @chaitanya89 可以给我一些代码sn-p吗?
  • SO 不是搜索引擎 .. 但您可以轻松找到数千篇关于如何在 java 中使用 JSON 的教程。

标签: java arrays json


【解决方案1】:
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/

【讨论】:

    【解决方案2】:
    org.json.simple.JSONArray yourArray = new org.json.simple.JSONArray();
    org.json.JSONArray arr = new org.json.JSONArray(yourArray);
    

    【讨论】:

    • 请添加一些文字来描述您的帖子如何回答问题。谢谢。
    猜你喜欢
    • 2020-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-04
    • 1970-01-01
    相关资源
    最近更新 更多