如何从提供的JSON获取值?

时间:2019-10-17 12:26:06

标签: c# json unity3d

我正在尝试从以下JSON数组中获取id的值。

[
  {
    "SaveValues": [
      {
        "id": 1,
        "allposition": {
          "x": -5.02046537399292,
          "y": 4.489554405212402
        },
        "allrotation": {
          "x": 0,
          "y": 0,
          "z": 0,
          "w": 1
        },
        "allscale": {
          "x": 1,
          "y": 1
        },
        "linepos0": {
          "x": 0,
          "y": 0,
          "z": 0
        },
        "linepos1": {
          "x": 0,
          "y": 0,
          "z": 0
        },
        "movetype": 1
      },
      {
        "id": 2,
        "allposition": {
          "x": -4.485479354858398,
          "y": -4.903786659240723
        },
        "allrotation": {
          "x": 0,
          "y": 0,
          "z": 0,
          "w": 1
        },
        "allscale": {
          "x": 1,
          "y": 1
        },
        "linepos0": {
          "x": 0,
          "y": 0,
          "z": 0
        },
        "linepos1": {
          "x": 0,
          "y": 0,
          "z": 0
        },
        "movetype": 1
      }
    ],
    "NoteValues": [
      {
        "movenumber": 1,
        "notemsg": "Attack"
      }
    ]
  },
  {
    "SaveValues": [
      {
        "id": 1,
        "allposition": {
          "x": -5.02046537399292,
          "y": 4.489554405212402
        },
        "allrotation": {
          "x": 0,
          "y": 0,
          "z": 0,
          "w": 1
        },
        "allscale": {
          "x": 1,
          "y": 1
        },
        "linepos0": {
          "x": 0,
          "y": 0,
          "z": 0
        },
        "linepos1": {
          "x": 0,
          "y": 0,
          "z": 0
        },
        "movetype": 2
      },
      {
        "id": 1,
        "allposition": {
          "x": 2.780655860900879,
          "y": 4.399117469787598
        },
        "allrotation": {
          "x": 0,
          "y": 0,
          "z": 0,
          "w": 1
        },
        "allscale": {
          "x": 1,
          "y": 1
        },
        "linepos0": {
          "x": 0,
          "y": 0,
          "z": 0
        },
        "linepos1": {
          "x": 0,
          "y": 0,
          "z": 0
        },
        "movetype": 2
      },
      {
        "id": 2,
        "allposition": {
          "x": -4.485479354858398,
          "y": -4.903786659240723
        },
        "allrotation": {
          "x": 0,
          "y": 0,
          "z": 0,
          "w": 1
        },
        "allscale": {
          "x": 1,
          "y": 1
        },
        "linepos0": {
          "x": 0,
          "y": 0,
          "z": 0
        },
        "linepos1": {
          "x": 0,
          "y": 0,
          "z": 0
        },
        "movetype": 2
      },
      {
        "id": 2,
        "allposition": {
          "x": 2.153193712234497,
          "y": -4.7106828689575195
        },
        "allrotation": {
          "x": 0,
          "y": 0,
          "z": 0,
          "w": 1
        },
        "allscale": {
          "x": 1,
          "y": 1
        },
        "linepos0": {
          "x": 0,
          "y": 0,
          "z": 0
        },
        "linepos1": {
          "x": 0,
          "y": 0,
          "z": 0
        },
        "movetype": 2
      }
    ],
    "NoteValues": [
      {
        "movenumber": 2,
        "notemsg": "Defend"
      }
    ]
  }
]

最初,我只想显示SaveValue [0]数组中的“ id”,然后单击按钮,然后单击SaveValue [1]数组中的“ id”。

for (int i = 0;i<1;i++)
{
    Debug.Log("Jnode values count " + JNode[i]["SaveValues"].Count);
    for (int j = 0;j<JNode[i]["SaveValues"].Count;j++)
    {
        int id = JNode[i]["SaveValues"][j]["id"];
        Debug.Log("id values --- " + id);
    }
}

实际上我正在使用以下类创建JSON文件。问题在于如何使用相同的JSON类检索它。如何反序列化它。

[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中循环并获取不同的(自定义)值。

1 个答案:

答案 0 :(得分:0)

最好创建代表JSON的类,以便可以使用JsonConvert.DeserializeObject<T>(string json)反序列化。 然后,您可以使用属性访问数据。

如果您只需要SaveValuesid的值,则您的类如下所示:

class DataObject
{
     public PlayerHandler[] SaveValues { get; set; }
}

迭代所有idsSaveValues)中的所有PlayerHandler的代码如下:

var deserialized = JsonConvert.DeserializeObject<DataObject[]>(json);

foreach (var dataObject in deserialized)
{
      foreach (var playerHandler in dataObject.SaveValues)
      {
          var id = playerHandler.Id;
          //Do with Id whatever you want
          Debug.Log(id.ToString);
      }
}

如果您需要SaveValue中的其他值,则可以将属性添加到SaveValue类中。 由于问题的更改而过时。

个人我几乎总是建议将JSON反序列化为对象,然后访问值,而不是使用JNode,JObject等来提高可读性。