0

I'm kinda new with json. So I want to make an array value inside a value of an array like below.

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

But unfortunately It's not working. It gives me an error when I tried to read the data.

I want to know if it's possible to have an array inside an array value. Thank you.

Ali Akbar
  • 39
  • 10
  • 1
    Your json is invalid. That's why you are getting that error. Check [here](http://json2csharp.com/) when you get error with your json. – Programmer Jul 28 '17 at 05:39
  • plus 1 for the useful link. So how do I write it correctly ? I have no idea how to achieve that. – Ali Akbar Jul 28 '17 at 05:44
  • 1
    What exactly do you want to save? I really can't help without knowing that. If you can put the class or example of what the class you want to save looks like then I can show you what the json should look like. – Programmer Jul 28 '17 at 05:48
  • Sorry it took me some time to visualize what I'm trying to achieve. [This](https://snag.gy/cb8RPQ.jpg) is the scheme in my real case. To achieve that, I have to write it something like this in [json](https://snag.gy/iYb1GO.jpg). And in C# I will make a function that receive the input(key) given in the inspector -> scan that input -> and if the input exist in json, return that value. So for example if write input inner[0], it will return "He is so scarry" – Ali Akbar Jul 28 '17 at 06:30
  • Check my answer. – Programmer Jul 28 '17 at 06:54
  • You have an error in your JSON. Should be: `value: [10, [2, 3, 5], 1]` (note comma after `10`) – clinton3141 Jul 28 '17 at 07:02
  • @AliAkbar Did you see the answer I left? Did it work? – Programmer Jul 28 '17 at 17:58

2 Answers2

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

}

Option2:

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

Option3:

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

I always advice people to create a class and generate json from it instead of trying to construct json from your head. It's easier if you do it this way and the generated json will always be valid and exaclty what you wanted.

This is what you want the json to look like:

enter image description here

Here is what the structure should look like:

[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;
}

Now, let's recreate what you have in that screenshot:

//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);

The generated Json result from Debug.Log:

{"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..."}}

See how I generated the valid json by creating a data structure of what you have in your screenshot, then creating new instance of it. That's the best way to do this. If this is not exactly what you wanted then your image is wrong. You can easily modify the data structure to get what you want.

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • Actually I don't know if it's working or not because I'm still trying to get the key from my code. I did some googling and I found this guy looks like facing the same [problem](https://stackoverflow.com/questions/22565077/javascript-get-json-key-name) like me. As you can see on his thread, he sometimes has different key (typeA and typeB) inside his data. He want to get the key name as a string to make a condition in his code. Unfortunately it looks like his problem was with javascript for web development, not unity. – Ali Akbar Jul 28 '17 at 18:27
  • I have no idea what you mean by "key"". Your original post is talking about nested array which has nothing to do with key. Also, the image in your question is also talking about array. I think you need to rephrase your question and make clear what exactly you are doing. – Programmer Jul 28 '17 at 18:32
  • You asked me if it's working or not and that's the answer. But if I look at your json structure it looks like what I want but I'm not sure if it's working or not to call in my code. I also not good in english so maybe it might confuse people about what the problem really is. – Ali Akbar Jul 29 '17 at 01:57