本文介绍了使用简单的json,将包含json数组的json对象转换为字符串数组(在Java中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于在找到此特定问题的实际答案之前已经关闭了此问题,因此请在 。

As this question has been closed before actual answer for this specific problem has been found, it is here.

在Java中,我正在使用。

In java, I am using the simplejson library to handle json.

我的json具有以下结构(被截断):

My json have this structure (truncated):

{

    "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数据:

I read the json data with the following code:

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

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

我正在尝试将列表json数组转换为带有以下内容的字符串数组:

And I am trying to convert the list json array into a string array, with the following:

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

但这会引发以下异常:

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

How can I convert the json array into a string array (String[]) ?

推荐答案

使用以下代码,希望它可以解决您的问题:

Use below code, Hopefully it will solved your problem:

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;

这篇关于使用简单的json,将包含json数组的json对象转换为字符串数组(在Java中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 10:57