【问题标题】:How to Deserialize a JSON into a list using SIMPLE JSON?如何使用 SIMPLE JSON 将 JSON 反序列化为列表?
【发布时间】:2019-10-19 09:41:42
【问题描述】:

我有以下 JSON。

[{"SaveValues":[{"id":1,"allposition":{"x":-4.888263702392578,"y":4.799035549163818},"allrotation":{"x":0.0,"y" :0.0,"z":0.0,"w":1.0},"allscale":{"x":1.0,"y":1.0},"linepos0":{"x":0.0,"y":0.0 ,"z":0.0},"linepos1":{"x":0.0,"y":0.0,"z":0.0},"movetype":1},{"id":2,"allposition": {"x":-4.607616424560547,"y":-4.033360004425049},"allrotation":{"x":0.0,"y":0.0,"z":0.0,"w":1.0},"allscale": {"x":1.0,"y":1.0},"linepos0":{"x":0.0,"y":0.0,"z":0.0},"linepos1":{"x":0.0," y":0.0,"z":0.0},"movetype":1}],"NoteValues":[{"movenumber":1,"notemsg":"ssd"}]},{"SaveValues":[{ "id":1,"allposition":{"x":-4.888263702392578,"y":4.799035549163818},"allrotation":{"x":0.0,"y":0.0,"z":0.0,"w ":1.0},"allscale":{"x":1.0,"y":1.0},"linepos0":{"x":0.0,"y":0.0,"z":0.0},"linepos1" :{"x":0.0,"y":0.0,"z":0.0},"movetype":2},{"id":2,"allposition":{"x":-4.607616424560547,"y" :-4.033360004425049},"allrotation":{"x":0.0,"y":0.0,"z":0.0,"w":1.0},"allscale":{"x":1.0,"y": 1.0},"linepos0":{"x":0.0,"y":0.0,"z":0.0},"linepos1":{"x":0.0,"y":0.0," z":0.0},"movetype":2},{"id":2,"allposition":{"x":4.328777313232422,"y":1.9337825775146485},"allrotation":{"x":0.0," y":0.0,"z":0.0,"w":1.0},"allscale":{"x":1.0,"y":1.0},"linepos0":{"x":0.0,"y" :0.0,"z":0.0},"linepos1":{"x":0.0,"y":0.0,"z":0.0},"movetype":2}],"NoteValues":[{"movenumber ":2,"notemsg":"ddf"}]}]

它们用于序列化的类如下所示。

[System.Serializable]
public class PlayerHandler 
{
    public int id; 
    public Vector2 allposition;
    public Quaternion allrotation;
    public Vector2 allscale;

    public Vector3 linepos0;
    public Vector3 linepos1;
    public int movetype;

    public PlayerHandler(int ids,Vector2 allpos,Quaternion allrot,Vector2 allscal,Vector3 Line0,Vector3 Line1,int Moves)
    {
        this.id = ids;
        this.allposition = allpos;
        this.allrotation = allrot;
        this.allscale = allscal;
        this.linepos0 = Line0;
        this.linepos1 = Line1;
        this.movetype = Moves;
    }

}

[System.Serializable]    
public class PlayerMovement
{
    public int movenumber;
    public string notemsg;

    public PlayerMovement(int Movenum,string Note)
    {
        this.movenumber = Movenum;
        this.notemsg = Note;    
    }
}

在加载方法中我试图反序列化 JSON。所以我创建了一个列表 List<PlayerHandler> SaveValuesDeserialize = new List<PlayerHandler>();

我希望从 Json 中添加 SaveValues。我尝试过的方法如下。

using (FileStream fs = new FileStream(Application.persistentDataPath + "/" + filename, FileMode.Open))
{
    fs.Dispose();
    jsonLoadstring = File.ReadAllText(Path.Combine(Application.persistentDataPath, filename));
    JSONNode JNode = SimpleJSON.JSON.Parse(jsonLoadstring);                

    var deseiralized = JNode[0]["NoteValues"][0]["notemsg"];//Here I am getting the deserialized value for testing.

    //What should I do to get the SaveValues inside the list

    for (int i = 0; i < JNode.Count; i++)
    {
        SaveValuesDeserialize.Add(JNode[i]["SaveValues"][i]);
    }
}

我希望使用简单的 JSON 来解决这个问题。我尝试使用其他方法来反序列化,这有点难以理解。所以选择这种方法来尝试。因为我得到了测试值,我希望我能得到使用 SIMPLEJSON 的值。

【问题讨论】:

    标签: c# json unity3d


    【解决方案1】:

    反序列化 json 的最简单方法通常是创建对 json 对象建模的类。您可以使用 json2csharp 创建类,或使用 Visual Studio(编辑 -> 选择性粘贴 -> 将 JSON 粘贴为类)。

    创建下面的类,然后您可以使用JsonConvert 将 json 反序列化到您的对象中。请注意,由于您只对 SaveValues 感兴趣,因此您不需要映射其他属性(例如 NoteValues)。

    反序列化为对象的代码:

    var myModel = JsonConvert.DeserializeJson<List<Response>>(json);
    
    // access by iterating through properties
    foreach (var item in response)
    {
        foreach (var saveValue in item.SaveValues)
        {
            // get all properties of saveValue...
            // saveValue.Allposition;
            // saveValue.Allrotation;
            // saveValue.Allscale;
            // ....
        }
    }
    

    这会将json反序列化为List&lt;Response&gt;,其中Response如下:

    public class Allposition
    {
        public double x { get; set; }
        public double y { get; set; }
    }
    
    public class Allrotation
    {
        public double x { get; set; }
        public double y { get; set; }
        public double z { get; set; }
        public double w { get; set; }
    }
    
    public class Allscale
    {
        public double x { get; set; }
        public double y { get; set; }
    }
    
    public class Linepos0
    {
        public double x { get; set; }
        public double y { get; set; }
        public double z { get; set; }
    }
    
    public class Linepos1
    {
        public double x { get; set; }
        public double y { get; set; }
        public double z { get; set; }
    }
    
    public class SaveValue
    {
        public int Id { get; set; }
        public Allposition Allposition { get; set; }
        public Allrotation Allrotation { get; set; }
        public Allscale Allscale { get; set; }
        public Linepos0 Linepos0 { get; set; }
        public Linepos1 Linepos1 { get; set; }
        public int Movetype { get; set; }
    }
    
    public class Response
    {
        public List<SaveValue> SaveValues { get; set; }
        // NoteValues can safely be removed from model if you don't need the values
        public List<NoteValue> NoteValues { get; set; }
    }
    
    // optional
    public class NoteValue
    {
        public int Movenumber { get; set; }
        public string Notemsg { get; set; }
    }
    

    【讨论】:

      【解决方案2】:

      感谢您的回答 haldo 我还没有尝试过您的解决方案,但是我尝试了以下解决方案,它对我有用。我对类进行了一些更改以序列化数据,以便我可以使用它来反序列化它。

      public class ListContainer
      {
      
      public List<PlayerHandler> SaveValues;
      public List<PlayerMovement> NoteValues;
      
      
      public ListContainer(List<PlayerHandler> _saveVal, List<PlayerMovement> _noteVal)
      {
          SaveValues = _saveVal;
          NoteValues = _noteVal;
      
      }
      
      }
      
      
      [System.Serializable]
      public class PlayerHandler 
      {
      public int id; 
      public Vector2 allposition;
      public Quaternion allrotation;
      public Vector2 allscale;
      
      public Vector3 linepos0;
      public Vector3 linepos1;
      public int movetype;
      
      
      public PlayerHandler(int ids,Vector2 allpos,Quaternion allrot,Vector2 allscal,Vector3 Line0,Vector3 Line1,int Moves)
      
      {
          this.id = ids;
          this.allposition = allpos;
          this.allrotation = allrot;
          this.allscale = allscal;
          this.linepos0 = Line0;
          this.linepos1 = Line1;
          this.movetype = Moves;
      }
      
      
      }
      
      [System.Serializable]
      public class PlayerMovement
      {
      public int movenumber;
      public string notemsg;
      
      public PlayerMovement(int Movenum,string Note)
      {
          this.movenumber = Movenum;
          this.notemsg = Note;
      
      }
      
      }
      
      
      //Added to retrieve data
      [System.Serializable]
      public class RootObject
      {
          public PlayerHandler[] SaveValues;
          public PlayerMovement[] NoteValues;
      
          }
      
      //Added to retrieve data
      [System.Serializable]
      public class RootObjects
      {
      
      
       public RootObject[] Roots;
      }
      

      我对如何检索数据进行了一些更改。

      using (FileStream fs = new FileStream(Application.persistentDataPath + "/" + filename, FileMode.Open))
      {
      
      
      fs.Dispose();
      
      
      jsonLoadstring = File.ReadAllText(Path.Combine(Application.persistentDataPath, filename));
      
      
      string saveString = "{ \"Roots\" : " + jsonLoadstring + "}";
      
      GetItems = JsonUtility.FromJson<RootObjects>(saveString);
      //Loop GetItems to retrieve data.
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-14
        • 2022-01-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多