unity中如何从json获取数组?

问题描述 投票:0回答:1

我得到了一个统一的json str:

{"action":"result","code":"0","data":"{\"seg_id\":2,\"cn\":{\"st\":{\"rt\":[{\"ws\":[{\"cw\":[{\"sc\":0.00,\"w\":\"今天\",\"wp\":\"n\",\"rl\":\"0\",\"wb\":9,\"wc\":0.00,\"we\":52}],\"wb\":9,\"we\":52},{\"cw\":[{\"sc\":0.00,\"w\":\"天气\",\"wp\":\"n\",\"rl\":\"0\",\"wb\":53,\"wc\":0.00,\"we\":92}],\"wb\":53,\"we\":92},{\"cw\":[{\"sc\":0.00,\"w\":\"怎么样\",\"wp\":\"n\",\"rl\":\"0\",\"wb\":93,\"wc\":0.00,\"we\":156}],\"wb\":93,\"we\":156},{\"cw\":[{\"sc\":0.00,\"w\":\"?\",\"wp\":\"p\",\"rl\":\"0\",\"wb\":156,\"wc\":0.00,\"we\":156}],\"wb\":156,\"we\":156}]}],\"bg\":\"160\",\"type\":\"0\",\"ed\":\"1900\"}},\"ls\":true}","desc":"success","sid":"rta02434f94@dx5bb219816a261aba00"}

我创建类,使用

JsonUtility.fromJson
来解析它。

public class JsonData
{
    public string action;

    public string code;

    public Data data;

    public string desc;

    public string sid;
}

[Serializable]
public class Data
{
    public string seg_id;
    [Serializable]
    public class CN
    {
        [Serializable]
        public class ST
        {
            [Serializable]
            public class RT
            {
                [Serializable]
                public class WS
                {
                    [Serializable]
                    public class CW
                    {
                        public string w;
                        public string wp;
                    }

                    public CW[] cw;
                    public string wb;
                    public string we;
                }

                public WS[] ws;
            }
            public RT rt;
            public string bg;
            public string type;
            public string ed;
        }

        public ST st;
    }
    public CN cn;
    public string ls;
}

但结果却是空的。 我使用

JsonUtility.toJson
,它无法获取任何数据值。 但是,我想获取json中的值
w
,我该如何获取它?

我测试了其他方法,但仍然可以解析它。

c# json unity-game-engine
1个回答
0
投票

我看到 Json 没有格式化,我进行了修复。我用 Json 生成了 C# 文件,发现我可以获取其中的数组值。我使用 Newtonsoft 程序集将 Json 反序列化为类。进行更改后,我可以看到 w 属性中填充的值,如下所示

请找到用于将 Json 反序列化为 C# 类的类文件。

public class Cn
{
    public St st { get; set; }
}

public class Cw
{
    public double sc { get; set; }
    public string w { get; set; }
    public string wp { get; set; }
    public string rl { get; set; }
    public int wb { get; set; }
    public double wc { get; set; }
    public int we { get; set; }
}

public class Data
{
    public int seg_id { get; set; }
    public Cn cn { get; set; }
    public bool ls { get; set; }
}

public class JsonData
{
    public string action { get; set; }
    public string code { get; set; }
    public Data data { get; set; }
    public string desc { get; set; }
    public string sid { get; set; }
}

public class Rt
{
    public List<W> ws { get; set; }
}

public class St
{
    public List<Rt> rt { get; set; }
    public string bg { get; set; }
    public string type { get; set; }
    public string ed { get; set; }
}

public class W
{
    public List<Cw> cw { get; set; }
    public int wb { get; set; }
    public int we { get; set; }
}

请找到将Json反序列化为C#类文件的代码

string sampleJson = @"{
  ""action"":""result"",
  ""code"":""0"",
  ""data"":{
    ""seg_id"":2,
    ""cn"":
    {
        ""st"": {
          ""rt"":[
            {""ws"":
              [
                {""cw"":
                  [
                    {
                      ""sc"":0.00,
                      ""w"":""今天"",
                      ""wp"":""n"",
                      ""rl"":""0"",
                      ""wb"":9,
                      ""wc"":0.00,
                      ""we"":52}
                  ],
                ""wb"":9,
                ""we"":52
              },
              {
                ""cw"":[{
                    ""sc"":0.00,
                    ""w"":""天气"",
                    ""wp"":""n"",
                    ""rl"":""0"",
                    ""wb"":53,
                    ""wc"":0.00,
                    ""we"":92
                  }],
                  ""wb"":53,
                  ""we"":92
              },
              {
                ""cw"":[{
                  ""sc"":0.00,
                  ""w"":""怎么样"",
                  ""wp"":""n"",
                  ""rl"":""0"",
                  ""wb"":93,
                  ""wc"":0.00,
                  ""we"":156
                }],
                ""wb"":93,
                ""we"":156
              },
              {
                ""cw"":[{
                  ""sc"":0.00,
                  ""w"":""?"",
                  ""wp"":""p"",
                  ""rl"":""0"",
                  ""wb"":156,
                  ""wc"":0.00,
                  ""we"":156
                }],
                ""wb"":156,
                ""we"":156
              }]
            }]
            ,""bg"":""160"",
            ""type"":""0"",
            ""ed"":""1900""
        }
      },
    ""ls"":true
  },
  ""desc"":""success"",
  ""sid"":""rta02434f94@dx5bb219816a261aba00""
}";

JsonData myDeserializedClass = JsonConvert.DeserializeObject<JsonData>(sampleJson);
Console.WriteLine("Press any key...");
Console.ReadKey();
© www.soinside.com 2019 - 2024. All rights reserved.