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. Dismiss Notice

JsonUtility.FromJson - Unexpected Node Type

Discussion in 'Editor & General Support' started by Atum-The-Creator, Aug 22, 2020.

  1. Atum-The-Creator

    Atum-The-Creator

    Joined:
    May 27, 2016
    Posts:
    2
    Hello!

    I am having trouble trying to use JsonUtility.FromJson to properly separate and iterate through a single json with "mixed" types. I keep receiving an Unexpected Node Type exception.

    I have structs for both world and player, but I am passing the same JSON (see below) to both of the structs.

    Code (json):
    1. {
    2.     "World": [
    3.         {
    4.             "world": "200",
    5.             "light": "0",
    6.             "temp": "1000",
    7.         }
    8.     ],
    9.     "Player": [
    10.         {
    11.             "gold": "200",
    12.             "goldBox": "0",
    13.             "maxHp": "1000",
    14.             "curHp": "995",
    15.             "hpPrice": "1000",
    16.             "firstGame": "False"
    17.         }
    18.     ]
    19. }

    I am trying to separate this data and send the world json array to the world struct, and the player json array to the player struct.

    Any help would be much appreciated! Thank you!
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    You need c# classes that match the pattern of the json data. So in this case, some kind of wrapper class that has two arrays, one named World and one named Player, and then two other classes for each of the types in the arrays.
     
  3. Atum-The-Creator

    Atum-The-Creator

    Joined:
    May 27, 2016
    Posts:
    2
    Okay PraetorBlue Thank you for your response!