【问题标题】:Unity JSON string array into objects from cUrl [duplicate]Unity JSON字符串数组从cUrl转换为对象[重复]
【发布时间】:2018-09-22 16:26:27
【问题描述】:

我不知道如何做到这一点。我从这样的 www 请求中获得了一些用户:

Dictionary<string, string> headers = new Dictionary<string, string>();
headers.Add("Authorization", "Basic "+System.Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes("user*pass")));
WWW www = new WWW("https://somedomain.com:8000/users", null, headers);
yield return www;
Debug.Log(www.text);  

调试返回:

[{"user_id":"ho896ty6","user_name":"Mikje Flanders","age":43},{"user_id":"ft357hj","user_name":"Anna Simpson","age":56}]

现在,我有一个像这样的对象:

public class userData
{
    string user_id;
    string user_name;
    int age;
}

我想将数据放入其中,但不确定 json 何时是一个数组。我试过这样,但没有运气:

userData thisUser = JsonUtility.FromJson<userData>(www.text);

希望有人可以帮助我,并在此先感谢 :-)

【问题讨论】:

  • 您的 json 始终是一个数组,或者有时可能是一个对象?
  • 你不需要 NewtonSoft 来反序列化数组。带有简单包装器的 JsonUtility 应该可以做到

标签: c# arrays unity3d json-deserialization


【解决方案1】:

1) 从Nuget Package Manager 安装Newtonsoft.json 并添加对您程序的引用 喜欢using Newtonsoft.Json;

2) 这是你的用户模型类

public class User
{
    public string user_id { get; set; }
    public string user_name { get; set; }
    public int age { get; set; }
}

3) 然后将您的 json 反序列化为您的模型,例如

class Program
{
    static void Main(string[] args)
    {
        //Sample json i get in variable
        var json = @"[{'user_id':'ho896ty6','user_name':'Mikje Flanders','age':43},{'user_id':'ft357hj','user_name':'Anna Simpson','age':56}]";

        //This line convert your string json to c# object
        List<User> userList = JsonConvert.DeserializeObject<List<User>>(json);

        //Loop through to get each object inside users list
        foreach (User user in userList)
        {
            Console.WriteLine($"user_id: {user.user_id},  user_name: {user.user_name}, age: {user.age}");
        }

        Console.ReadLine();
    }
}

输出:

尝试一次可能对你有帮助。

【讨论】:

  • @Mansa,查看答案可能对你有帮助:)
  • 完美,它完成了工作:-)
【解决方案2】:

您必须使用分隔符解析 Json,除非您在这种情况下使用 Visual Studio,否则在安装此软件包的情况下检查您的 nuget 以获取 Newtonsoft.Json

  Using NewtonSoft.Json;

List udat = JsonConvert.DeserializeObject>(json);

   foreach( userData data in udat){
   //do something here to each item;
   }

使用jsoneditoronline

[
  {
    "user_id": "ho896ty6",
    "user_name": "Mikje Flanders",
    "age": 43
  },
  {
    "user_id": "ft357hj",
    "user_name": "Anna Simpson",
    "age": 56
  }
]

我稍微清理了您的代码,以便您可以看到自己在做什么。 为您的课程添加了一些 getter 和 setter...

看起来您的请求中返回了多个 userData,因此您可能需要一个列表或数组。如果您需要帮助,请告诉我!

  public class userData
    {
       public string user_id {get;set;}
       public string user_name{get;set;}
       public int age{get;set;}
    }

public class userList
{
public List<userData> users{get;set;}
}

【讨论】:

  • 我遇到了与 foreach 中的“udat”有关的问题(udat 中的 userData 数据){}。它下面有一条红线,我无法让它工作......希望你能提供帮助。
  • 我的坏 udat.users
  • 好吧,解决了这个问题,但现在我遇到了这个问题:JsonSerializationException: Cannot deserialize the current JSON array (eg [1,2,3]) into type 'userList' because type requires a JSON对象(例如 {"name":"value"})正确反序列化。要修复此错误,请将 JSON 更改为 JSON 对象(例如 {"name":"value"})或将反序列化类型更改为数组或实现集合接口的类型(例如 ICollection、IList),例如 List 可以从 JSON 数组反序列化。 JsonArrayAttribute 也可以添加到类型中,以强制它从 JSON 数组中反序列化。
  • 我更新了代码以直接解析为列表,确实注意到我下面的答案在同一轨道上@ershoaib!
  • 完美,它完成了工作:-)
猜你喜欢
  • 1970-01-01
  • 2014-08-20
  • 1970-01-01
  • 2018-06-26
  • 1970-01-01
  • 2013-08-29
  • 2014-06-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多