#Json format

1 messages · Page 1 of 1 (latest)

full forge
#

Hello guys!
I have a question regarding the json format and how to convert from object to json string.

[Serializable]
protected class JsonWrapper
{
    public string name;
    public string surname;
    public string occupation;
    public List<List<string>> requests = new();
}

I have a JsonWrapper object as follows which contains nested list as a member

I need to convert this guys into json string that looks like this

{
  "name": "some_name",
  "surname": "some_surname",
  "occupation": "dentist",
  "requests": [
    ["money", "500"],
    ["help", "psychological"]
  ]
}

The code that I use to do that is as follows:

JsonWrappet toJson = new JsonWrapper();
toJson.name = "some_name";
toJson.surname = "some_surname";
toJson.occupation = "dentist";
toJson.requests = new();

// Creating a list
List<string> firstReq = new();
firstReq.Add("money");
firstReq.Add("500");

// Convert to Json
string s = UnityEngine.JsonUtility.ToJson(toJson);
print(s);

The output of this is as follows:
{"name":"some_name","surname":"some_surname","occupation":"dentist"}

I don't see anything from requests

Can anyone give me a hand regarding this issue?

#

@loud plinth I would be happy if you take a look at this

sly gyro
#

You would have to change the object structure, or use a different serialization framework, such as newtonsoft JSON

full forge
sly gyro
#

make a List<SomeObject>

#

where SomeObject is a serializable class or struct with a list in it

loud plinth