0

I want to create a JSON Dictonary (key and value are both strings), that I can send via TCP. The second key contains a Class with just a single List of Objects. I try to convert it to JSON, so I am able to send it.

 AllCharacterActions characterActions = new AllCharacterActions();
// fill List in this class with content   
JsonDictionary send = new JsonDictionary ();
    send ["command"] = "OrderedFightPlayersList";
    send ["ClassWithList"] = JsonUtility.ToJson(characterActions);

But if I debug.Log the send ["ClassWithList"], it says {} . This is the code of the two classes. The second one is the one I try to convert. The first one is the Class that should be in the list (4 times). I know the list with the 4 classes is in it, before I convert it.

public class CharacterAction
{
    public int[] targetID;
    public int characterID;
    public string itemName;
    public string action;
}
[Serializable]
public class AllCharacterActions
{
    public List<CharacterAction> allCharacterActions = new List<CharacterAction>();

    public void Add(CharacterAction characterAction)
    {
        allCharacterActions.Add(characterAction);
    }
} 

I'm using C# and Unity.

Tobsi
  • 51
  • 1
  • 5
  • Are you sure you're referencing the correct variables here? In one snippet you're serializing `characterActions`, while in another you're populating `allCharacterActions`. – Serlite Feb 22 '18 at 19:25
  • Yes I thing its right. character Actions is the name of the Object that I created from this class. Maybe I should add it to the post. – Tobsi Feb 22 '18 at 19:49
  • I think https://stackoverflow.com/questions/41787091/unity-c-sharp-jsonutility-is-not-serializing-a-list is a closer match to this problem. – Colin Young Feb 23 '18 at 15:21

0 Answers0