Java - 从多个多维数组创建 JSON 对象

发布于 2024-12-04 07:54:38 字数 980 浏览 0 评论 0原文

我正在尝试将非常复杂的数组结构转换为 JSON 对象,但我不确定如何进行转换。结构如下:

String[] foo = new String[10];
String[][] bar = new String[10][8];
String[][] blargh = new String[8][2];
// Populate foo
foo[0] = "foo1";
// ... and so on
bar[0][0] = "bar1";
// ... and so on
blargh[0][0] = "blargh1;"
// ... and so on

然后:

public JSONObject createJSONObject() {
/* Now, I would like to create an object with the structue:
   [{
    foo[0] : {
        bar[0][0] : {
            // more key-pair values, including blargh[0][0] and blargh[0][1]
        },
        bar[0][1] : {
            // values of blargh[1][0] and blargh[1][1]
        },
        // etc...
    },
    foo[1] : {
        bar[1][0] : {
            /* primary index of bar will always match index of foo, as will the primary index of blargh */
        },
        // etc..
    },
    // etc..
    }]
    // return the JSON encoded object
}

这对我来说似乎相当复杂,所以请告诉我我的问题/代码/结构是否令人困惑或不清楚。

I'm trying to convert a very complex array structure to a JSON object, but I'm not sure how to work with the conversion. The structure is as follows:

String[] foo = new String[10];
String[][] bar = new String[10][8];
String[][] blargh = new String[8][2];
// Populate foo
foo[0] = "foo1";
// ... and so on
bar[0][0] = "bar1";
// ... and so on
blargh[0][0] = "blargh1;"
// ... and so on

Then:

public JSONObject createJSONObject() {
/* Now, I would like to create an object with the structue:
   [{
    foo[0] : {
        bar[0][0] : {
            // more key-pair values, including blargh[0][0] and blargh[0][1]
        },
        bar[0][1] : {
            // values of blargh[1][0] and blargh[1][1]
        },
        // etc...
    },
    foo[1] : {
        bar[1][0] : {
            /* primary index of bar will always match index of foo, as will the primary index of blargh */
        },
        // etc..
    },
    // etc..
    }]
    // return the JSON encoded object
}

This seems reasonably complex to me, so please tell me if my question/code/structure is confusing or not clear.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绿萝 2024-12-11 07:54:38

将其分解为可管理的块。创建了解如何单独构造每个嵌套对象的方法,然后在适当的时间调用它们。例如,类似:

public JSONObject createJSONObject() {
    JSONObject result = new JSONObject();
    for (int fooIndex = 0; fooIndex < foo.length; fooIndex++) {
        result.put(foo[fooIndex], createBarJsonObject(fooIndex));
    }

    return result;
}

private JSONObject createBarJsonObject(int index) {
    JSONObject result = new JSONObject();
    String[] keys = bar[index];
    for (int barIndex = 0; barIndex < keys.length; barIndex++) {
        result.put(keys[barIndex], createBlarghJsonObject(fooIndex));
    }

    return result;
}

private JSONObject createBlarghJsonObject(int index) {
    JSONObject result = new JSONObject();
    String[] keyValue = blargh[index];
    result.put(keyValue[0], keyValue[1]);

    return result;
}

Break it down into manageable chunks. Create methods that understand how to construct each nested object individually, and then call them at the appropriate times. For instance, something like:

public JSONObject createJSONObject() {
    JSONObject result = new JSONObject();
    for (int fooIndex = 0; fooIndex < foo.length; fooIndex++) {
        result.put(foo[fooIndex], createBarJsonObject(fooIndex));
    }

    return result;
}

private JSONObject createBarJsonObject(int index) {
    JSONObject result = new JSONObject();
    String[] keys = bar[index];
    for (int barIndex = 0; barIndex < keys.length; barIndex++) {
        result.put(keys[barIndex], createBlarghJsonObject(fooIndex));
    }

    return result;
}

private JSONObject createBlarghJsonObject(int index) {
    JSONObject result = new JSONObject();
    String[] keyValue = blargh[index];
    result.put(keyValue[0], keyValue[1]);

    return result;
}
樱花细雨 2024-12-11 07:54:38

您不是在创建数组,而是在创建对象。伪代码中的封闭数组没有意义。

对象:{ key0:val0, key1:val1 }

数组:[ val0, val1, val2 ]

{ // start object
    key_foo0 : // foo[0] string as a key
    { // new object as a value
        key_bar0_0 :
        {
            key_blargh0_0: val_blargh0_1, // blargh[0][1] as value
            key_bleuuw0_0: val_bleuuw0_1
        }
    }
} // end object

You're not creating arrays, you're creating objects. There's no point in the enclosing array in your pseudo code.

Object: { key0:val0, key1:val1 }

Array: [ val0, val1, val2 ]

{ // start object
    key_foo0 : // foo[0] string as a key
    { // new object as a value
        key_bar0_0 :
        {
            key_blargh0_0: val_blargh0_1, // blargh[0][1] as value
            key_bleuuw0_0: val_bleuuw0_1
        }
    }
} // end object
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文