【问题标题】:Using simple json, transform a json object containing a json array into an array of strings (in java) [duplicate]使用简单的json,将包含json数组的json对象转换为字符串数组(在java中)[重复]
【发布时间】:2020-01-06 10:53:58
【问题描述】:

由于这个问题在找到这个具体问题的实际答案之前已经关闭,所以它是here

在java中,我使用simplejson library来处理json。

我的 json 有这个结构(截断):

{

    "assembling-tags": {
        "list": [
            "G_StaticCushion_R",
            "G_CommutationPosition_R",
            "G_PlastificationPlasticisingDuration_R",
            "G_PlastificationScrewPositionAfter_R",
            "G_CommutationPressure_R",
            "G_DynamicCommutationDuration_R",
            "G_DynamicLockingToolDuration_R",
            "G_CycleTime_R",
            "CYCLE_TIME",
            "G_ClosureSecurityClosingToolDuration_R"
        ]
    },

我用下面的代码读取了json数据:

try (FileReader reader = new FileReader(
                "/home/hduser/eclipse-workspace/db-simulatiob/src/generator-config.json")) {

            JSONObject obj = (JSONObject) jsonParser.parse(reader);

我正在尝试将列表 json 数组转换为字符串数组,如下所示:

String[] aTag = (String[]) ((JSONObject) obj.get("assembling-tags")).get("list");

但这会引发以下异常:

线程“主”java.lang.ClassCastException 中的异常: org.json.simple.JSONArray 无法转换为 [Ljava.lang.String;

如何将 json 数组转换为字符串数组 (String[])?

【问题讨论】:

  • 这能回答你的问题吗? Convert JSONArray to String Array
  • 异常是想告诉你一些事情,你看了吗?
  • @Smile 不,因为我想要一个 String[] 而不是 ArrayList。 (将为此详细信息编辑问题)。我也在使用 jsonsimple,所以我有一个 JSON 对象(参见代码),而不是字符串。
  • @f1sh 是的。而且我不知道如何解决这个问题。
  • 您是否阅读了所有关于该问题的答案。第二个答案展示了如何创建一个字符串数组。

标签: java arrays json string


【解决方案1】:

使用下面的代码,希望它能解决你的问题:

String jstr = "{\r\n" + 
                "\r\n" + 
                "    \"assembling-tags\": {\r\n" + 
                "        \"list\": [\r\n" + 
                "            \"G_StaticCushion_R\",\r\n" + 
                "            \"G_CommutationPosition_R\",\r\n" + 
                "            \"G_PlastificationPlasticisingDuration_R\",\r\n" + 
                "            \"G_PlastificationScrewPositionAfter_R\",\r\n" + 
                "            \"G_CommutationPressure_R\",\r\n" + 
                "            \"G_DynamicCommutationDuration_R\",\r\n" + 
                "            \"G_DynamicLockingToolDuration_R\",\r\n" + 
                "            \"G_CycleTime_R\",\r\n" + 
                "            \"CYCLE_TIME\",\r\n" + 
                "            \"G_ClosureSecurityClosingToolDuration_R\"\r\n" + 
                "        ]\r\n" + 
                "    }}";

        JSONObject obj  = new JSONObject(jstr);
        JSONArray jarr = obj .getJSONObject("assembling-tags").getJSONArray("list");
        String[] aTag =new String[jarr.length()];
        for(int i=0; i<aTag.length; i++) {
            aTag[i]=jarr.optString(i);
        } // aTag is ready to use 

        // Here I just print the output
        for(int i=0; i<aTag.length; i++) {
            System.out.println("jsonArray to String array:"+aTag[i]); 
        }

下面也导入:

import org.json.JSONArray;
import org.json.JSONObject;

【讨论】:

  • 我正在使用 jsonsimple。 JSONObject 没有方法 getJSONObject :S
猜你喜欢
  • 2015-06-08
  • 1970-01-01
  • 1970-01-01
  • 2018-06-26
  • 2021-01-16
  • 2011-05-21
  • 1970-01-01
  • 2014-01-23
  • 1970-01-01
相关资源
最近更新 更多