Search Unity

How to get values from the following JSON?

Discussion in 'Scripting' started by zyonneo, Oct 17, 2019.

  1. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    386
    Hi I am trying to get the value of 'id' from the following JSON.Initially I am trying to get the 'id' from array "SaveValues" ie from SaveValues[0] array.Next from SaveValues[1] array.

    Json of the above image given below
    The code I tried is
    Code (CSharp):
    1. for (int i = 0;i<1;i++)
    2.                 {
    3.                      Debug.Log("Jnode values " + JNode[i]["SaveValues"][0][i].ToString());
    4.                     for (int j = 0;j<JNode[i]["SaveValues"].Count;j++)
    5.                     {
    6.                        
    7.                     int id = JNode[i]["SaveValues"][j]["id"];
    8.                     Debug.Log("id values --- " + id);
    9.                     }
    10.              }
     
  2. nilsdr

    nilsdr

    Joined:
    Oct 24, 2017
    Posts:
    374
    Here's a list of structs which represent your data. Use JSON utility to cast your data to these structs:

    https://docs.unity3d.com/ScriptReference/JsonUtility.FromJson.html

    That will make your life a whole lot easier:

    I think you'll have to give a name to your rootobject

    Code (CSharp):
    1.  
    2. [Serializable]
    3. public struct Allposition
    4. {
    5.     public double x;
    6.     public double y;
    7. }
    8.  
    9. [Serializable]
    10. public struct Allrotation
    11. {
    12.     public double x;
    13.     public double y;
    14.     public double z;
    15.     public double w;
    16. }
    17.  
    18. [Serializable]
    19. public struct Allscale
    20. {
    21.     public double x;
    22.     public double y;
    23. }
    24.  
    25. [Serializable]
    26. public struct Linepos0
    27. {
    28.     public double x;
    29.     public double y;
    30.     public double z;
    31. }
    32.  
    33. [Serializable]
    34. public struct Linepos1
    35. {
    36.     public double x;
    37.     public double y;
    38.     public double z;
    39. }
    40.  
    41. [Serializable]
    42. public struct SaveValue
    43. {
    44.     public int id;
    45.     public Allposition allposition;
    46.     public Allrotation allrotation;
    47.     public Allscale allscale;
    48.     public Linepos0 linepos0;
    49.     public Linepos1 linepos1;
    50.     public int movetype;
    51. }
    52.  
    53. [Serializable]
    54. public struct NoteValue
    55. {
    56.     public int movenumber;
    57.     public string notemsg;
    58. }
    59.  
    60. [Serializable]
    61. public struct RootObject
    62. {
    63.     public List<SaveValue> SaveValues;
    64.     public List<NoteValue> NoteValues;
    65. }
    66.  
     
    zyonneo likes this.
  3. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    386
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [System.Serializable]
    6. public class PlayerHandler
    7. {
    8.     public int id;
    9.     public Vector2 allposition;
    10.     public Quaternion allrotation;
    11.     public Vector2 allscale;
    12.  
    13.     public Vector3 linepos0;
    14.     public Vector3 linepos1;
    15.     public int movetype;
    16.  
    17.  
    18.     public PlayerHandler(int ids,Vector2 allpos,Quaternion allrot,Vector2 allscal,Vector3 Line0,Vector3 Line1,int Moves)
    19.  
    20.     {
    21.         this.id = ids;
    22.         this.allposition = allpos;
    23.         this.allrotation = allrot;
    24.         this.allscale = allscal;
    25.         this.linepos0 = Line0;
    26.         this.linepos1 = Line1;
    27.         this.movetype = Moves;
    28.     }
    29.  
    30.  
    31.  
    32. }
    33.  
    34. [System.Serializable]
    35. public class PlayerMovement
    36. {
    37.     public int movenumber;
    38.     public string notemsg;
    39.  
    40.     public PlayerMovement(int Movenum,string Note)
    41.     {
    42.         this.movenumber = Movenum;
    43.         this.notemsg = Note;
    44.  
    45.     }
    46.  
    47. }
    48.  
    I already created this class to create the above .json file.I am looking to get back the values.
     
  4. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    386
    How to use the same class to deserialize it?
    Code (CSharp):
    1. for (int i = 0; i < JNode.Count; i++)
    2.                 {
    3.                     DeserialzeList1.Add(JNode[i]["SaveValues"]);
    4.  
    5.                 }
    6. //How to get the JSON into the list?
     
  5. nilsdr

    nilsdr

    Joined:
    Oct 24, 2017
    Posts:
    374
    Code (CSharp):
    1. RootObject ro = JsonUtility.FromJson<RootObject>(jsonString);
    However your root is an unnamed array which is not supported by the JsonUtility. So change the json structure to be an object.
     
  6. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    386
    How to save everything as a single JSON array without multiple arrays.This is confusing.