Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Cannot deserialize JSON object

Discussion in 'Scripting' started by qcw27710, Nov 16, 2019.

  1. qcw27710

    qcw27710

    Joined:
    Jul 9, 2019
    Posts:
    139
    Your typical error:

    JsonSerializationException: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'blablabla' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.


    I went to JSONLint which claims my JSON is valid.
    Sometime back one of you wondered whether there was a "BOM" in my file, I don't know, so here are the hexes:
    isbom.png

    I have Debug.Logged the value directly one line before attempt of deserialization (the location where the error is thrown). Console shows indeed a string and that string is my JSON string.

    Weirdly enough I have a general function that handles this (it skips loops, read files and then deserializes it), this is 3th line that throws an error. I used this function twice before and no errors, but it somehow is stuck on this line.

    So, what should I do?

    1. Yes, the path is correct, otherwise it wouldn't read the JSON into console log.
    2. Nothing is changed between them, its a closed function that simply performs some internal processing.
     
    Last edited: Nov 16, 2019
  2. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,315
    Assuming that you're using JSONUtility -- JSONUtility doesn't support deserializing raw arrays (eg.
    [1,2,3]
    ), even though that is technically a valid JSON object. In order to deserialize lists or arrays, you need to place them inside of another object and serialize that, eg.

    Code (CSharp):
    1. [Serializable]
    2. class ListWrapper {
    3.   public List<int> list;
    4. }
    Which will give you JSON along the lines of
    { "list": [1,2,3] }
    .

    This is what the error was referring to.
     
  3. qcw27710

    qcw27710

    Joined:
    Jul 9, 2019
    Posts:
    139
    I'm so sorry, I forgot to include the line that causes this mess:
    return JsonConvert.DeserializeObject<T>(data);

    Does this change your answer?
     
  4. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,315
    JsonConvert can handle deserializing lists, but this also isn't a Unity-specific issue. Here is a StackOverflow question about this error.
     
  5. qcw27710

    qcw27710

    Joined:
    Jul 9, 2019
    Posts:
    139
    Ah, I see. Instead of defining it as single item, I defined the T to be a List<T>. Now it works. Thanks.

    I doubt more than 10% of Scripting forum's threads are actually about Unity-specific issues. I believe majority, including myself, use this forum as means to get help from good-willed people in any C# questions that are involved in creation of a videogame in Unity. Major of our troubles don't come from misuse of Unity, but lack of understanding of C#.