【问题标题】:Is it possible to have nested array json?是否有可能有嵌套数组json?
【发布时间】:2017-07-28 05:37:22
【问题描述】:

我对 json 有点陌生。所以我想在一个数组的值中创建一个数组值,如下所示。

{
    "id": 0,
    "title": "LEVEL",
    "value": [10[2,3,5], 1]
}

但不幸的是,它不起作用。当我尝试读取数据时,它给了我一个错误。

我想知道是否可以在数组值中包含一个数组。谢谢。

【问题讨论】:

  • 您的 json 无效。这就是您收到该错误的原因。当您的 json 出现错误时,请检查 here
  • 为有用的链接加 1。那么我该如何正确写呢?我不知道如何实现。
  • 您到底想保存什么?不知道这一点,我真的无能为力。如果您可以将要保存的类的类或示例放在一起,那么我可以向您展示 json 应该是什么样的。
  • 抱歉,我花了一些时间来想象我想要实现的目标。 This 是我真实案例中的方案。为了实现这一点,我必须在json 中这样写。在 C# 中,我将创建一个函数来接收检查器中给出的输入(键)-> 扫描该输入-> 如果输入存在于 json 中,则返回该值。所以例如如果写输入inner[0],它会返回“He is so Scarry”
  • 检查我的答案。

标签: json unity3d litjson


【解决方案1】:

我总是建议人们创建一个类并从中生成 json,而不是尝试从你的头脑中构造 json。如果您这样做会更容易,并且生成的 json 将始终有效并且完全符合您的要求。

这是您希望 json 的样子:

结构应该是这样的:

[Serializable]
public class Dialog
{
    public Human human;
    public NonHuman nonHuman;
}

[Serializable]
public class Human
{
    public Inner inner;
    public Outer outer;
}

[Serializable]
public class NonHuman
{
    public string val;
}

[Serializable]
public class Inner
{
    public string[] val;
}

[Serializable]
public class Outer
{
    public string[] val;
}

现在,让我们重新创建屏幕截图中的内容:

//Create new dialog
Dialog dialog = new Dialog();

//Create nonhuman
dialog.nonHuman = new NonHuman();
dialog.nonHuman.val = "Once upon a time...";

//Create human
dialog.human = new Human();

//Create human inner 
dialog.human.inner = new Inner();
dialog.human.inner.val = new string[2];
dialog.human.inner.val[0] = "He is so scary";
dialog.human.inner.val[1] = "She is so beautiful";

//Create human outer 
dialog.human.outer = new Outer();
dialog.human.outer.val = new string[2];
dialog.human.outer.val[0] = "Hey watch out !";
dialog.human.outer.val[1] = "Look at her har !";

//Convert to Json
string json = JsonUtility.ToJson(dialog);
//Show result in the Console tab
Debug.Log(json);

Debug.Log生成的Json结果:

{"human":{"inner":{"val":["He is so scary","She is so beautiful"]},"outer":{"val":["Hey watch out !","Look at her har !"]}},"nonHuman":{"val":"Once upon a time..."}}

看看我是如何生成有效的 json 的,方法是创建你在屏幕截图中的数据结构,然后创建它的新实例。这是最好的方法。如果这不是您想要的,那么您的图像是错误的。您可以轻松修改数据结构以获得您想要的。

【讨论】:

  • 实际上我不知道它是否有效,因为我仍在尝试从我的代码中获取密钥。我做了一些谷歌搜索,我发现这个人看起来和我一样面对problem。正如您在他的线程中看到的那样,他有时在他的数据中具有不同的键(typeA 和 typeB)。他想将键名作为字符串在他的代码中创建一个条件。不幸的是,看起来他的问题在于用于 Web 开发的 javascript,而不是统一性。
  • 我不知道您所说的“键”是什么意思。您的原始帖子是在谈论与键无关的嵌套数组。此外,您问题中的图像也在谈论数组。我认为你需要重新表述你的问题并明确你在做什么。
  • 你问我它是否有效,这就是答案。但是,如果我查看您的 json 结构,它看起来就像我想要的,但我不确定它是否在我的代码中调用。我的英语也不好,所以可能会让人们对问题的真正含义感到困惑。
【解决方案2】:
{
"id": 0,
"title": "LEVEL",
"value": [10, [2, 3, 5], 1]

}

选项2:

    {
    "id": 0,
    "title": "LEVEL",
    "value": [10, {
        "someKey": [2, 3, 5]
    }, 1]
}

选项3:

       {
    "id": 0,
    "title": "LEVEL",
    "value": [10, {
        "someKey": [2, {
            "someKey": [9, 1, 1]
        }, 5]
    }, 1]
   }

【讨论】:

  • 我想用 10 作为一个标志,上面有一些数组值。如果我理解正确的话,10, [2, 3, 5], 1 是一个单独的值。
  • JSON 是关于键值对的,你不能按字面意思做你想做的事
  • 我认为它几乎可以满足我的需求。如果我将 [2, 3, 5] 更改为 [2, {"somekeyagain": [9, 1, 1]}, 5] 怎么办?
  • 是的,你可以这样做。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-12
  • 2014-08-22
  • 2021-12-10
  • 1970-01-01
相关资源
最近更新 更多