Search Unity

[Solved] converting Json to instance of struct

Discussion in 'Scripting' started by Gerray2, Jan 23, 2020.

  1. Gerray2

    Gerray2

    Joined:
    Apr 24, 2018
    Posts:
    13
    I mistakenly postet this under the wrong subject. Guess this fits better under "scripting".


    Hi,
    I want to import the following example of a Json:
    Code (CSharp):
    1. {
    2.    "g":[
    3.       {
    4.          "m":"cube",
    5.          "s":{
    6.             "x":4,
    7.             "y":6.75,
    8.             "z":0.5
    9.         },
    10.          "p":{
    11.             "x":-6.75,
    12.             "y":20,
    13.             "z":154.7
    14.         },
    15.          "r":{
    16.             "x":0,
    17.             "y":0,
    18.             "z":90
    19.         }
    20.      },
    21.       {
    22.          "m":"cylinder",
    23.          "s":{
    24.             "x":4,
    25.             "y":30,
    26.             "z":4
    27.         },
    28.          "p":{
    29.             "x":-7,
    30.             "y":20,
    31.             "z":149
    32.         },
    33.          "r":{
    34.             "x":0,
    35.             "y":0,
    36.             "z":90
    37.         }
    38.      }
    39.   ]
    40. }

    This is my struct:

    Code (CSharp):
    1. [System.Serializable]
    2.     public struct ProductInstance
    3.     {
    4.         public List<G> g;
    5.  
    6.         [System.Serializable]
    7.         public struct G
    8.         {
    9.             public string m;
    10.             public v3 s;
    11.             public v3 p;
    12.             public v3 r;
    13.  
    14.             [System.Serializable]
    15.             public struct v3
    16.             {
    17.                 public float x;
    18.                 public float y;
    19.                 public float z;
    20.             }
    21.         }
    22.  
    23.         public static ProductInstance CreateFromJSON(string jsonString)
    24.         {
    25.             return JsonUtility.FromJson<ProductInstance>(jsonString);
    26.         }
    27.     }
    And this is how I call it inside a function:

    var pi = ProductInstance.CreateFromJSON(json);


    But all I get is a exception. "ArgumentException: JSON parse error: The document root must not follow by other values."

    Please help!
     
  2. jamespaterson

    jamespaterson

    Joined:
    Jun 19, 2018
    Posts:
    400
  3. Gerray2

    Gerray2

    Joined:
    Apr 24, 2018
    Posts:
    13
    Thank you! It works now.

    You brought me on the right path.
    I now use Newtonsoft.Json for conversion.

    Code (CSharp):
    1. public class Product
    2.         {
    3.             public List<G> g { get; set; }
    4.         }
    5.  
    6.         public class G
    7.         {
    8.             public string m { get; set; }
    9.             public v3 s { get; set; }
    10.             public v3 p { get; set; }
    11.             public v3 r { get; set; }
    12.         }
    13.  
    14.         public class v3
    15.         {
    16.             public double x { get; set; }
    17.             public double y { get; set; }
    18.             public double z { get; set; }
    19.         }
    And call the conversion with:


    var prods = new List<Product>();
    prods.Add(JsonConvert.DeserializeObject<Product>(myJsonString));
     
    jamespaterson likes this.