Writing List as Json ,Making a List in Json

Trying to write a string array inside a List in the Json format, have been trying my best but i’ve been getting a bug saying that the list is null, pretty sure it’s the way i’m writing my Json as the script bugging is literally

Texts.Count;

Currently writing the List as:

  "AppearanceTexts": [
    [ "First appearance text line 1", "First appearance text line 2" ],
    [ "Second appearance text line 1", "Second appearance text line 2" ]
  ],

,

You haven’t mentioned what json serializer / deserializer you’re using. If you use Unity’s JsonUtility, such a structure would not work. It always expects an object as the root element. If the json you posted is your whole json, then yes, it’s not valid json at all. You can check your json here (jsonlint).

So first of all to actually create valid json in the first place, you have to remove trailing commas, not all deserializers like that. Also you need an actual object around your whole thing. Something like this:

{
	"AppearanceTexts": [
		["First appearance text line 1", "First appearance text line 2"],
		["Second appearance text line 1", "Second appearance text line 2"]
	]
}

This is valid json, however Unity’s JsonUtility doesn’t really like nested arrays. Having an array of a class that in turn contains an array does work. Other serializers like Newtonsoft’s free Json.NET library can read such constructs. That’s why most use this instead. Though you have only provided minimal information on your case. What does your code that loads the json look like? Which serializer do you use? Do you actually have a class that represents this json structure? So a class with a public List called “AppearanceTexts”?