- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
模型
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class GetPeopleResult
{
public List<Person> people { get; set; }
public GetPeopleResult()
{
this.people = new List<People>();
}
public static GetPeopleResult CreateFromJSON(string jsonString)
{
return JsonUtility.FromJson<GetPeopleResult>(jsonString);
}
}
[System.Serializable]
public class Person
{
public long id { get; set; }
public string name { get; set; }
public string email { get; set; }
public string displayImageUrl { get; set; }
public Person()
{
}
public static Person CreateFromJSON(string jsonString)
{
return JsonUtility.FromJson<Person>(jsonString);
}
}
JSON
{
"people":
[{
"id":1,"name":"John Smith",
"email":"jsmith@acme.com",
"displayImageUrl":"http://example.com/"
}]
}
代码
string json = GetPeopleJson(); //This works
GetPeopleResult result = JsonUtility.FromJson<GetPeopleResult>(json);
调用FromJson后,result不为null,但people集合始终为空。
最佳答案
After the call to FromJson, result is not null, but the people collection is always empty.
那是因为 Unity 不支持属性 getter 和 setter。删除 { get; set; }
来自您要序列化的所有类,这应该可以修复您的空集合。
此外,this.people = new List<People>();
应该是 this.people = new List<Person>();
[System.Serializable]
public class GetPeopleResult
{
public List<Person> people;
public GetPeopleResult()
{
this.people = new List<People>();
}
public static GetPeopleResult CreateFromJSON(string jsonString)
{
return JsonUtility.FromJson<GetPeopleResult>(jsonString);
}
}
[System.Serializable]
public class Person
{
public long id;
public string name;
public string email;
public string displayImageUrl;
public Person()
{
}
public static Person CreateFromJSON(string jsonString)
{
return JsonUtility.FromJson<Person>(jsonString);
}
}
关于c# - 无法使用 JsonUtility 在 Unity 5.4 中反序列化 JSON。子集合始终为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40429004/
这个问题在这里已经有了答案: Serialize and Deserialize Json and Json Array in Unity (9 个回答) 关闭 5 年前。 这是我用于测试目的的 J
所以我一直在寻找将我的 Json 数组转换为 Unity 中的对象数组的方法。我从一个 2 年前的旧线程中找到了我的解决方案,但没有解释,但我很好奇它是如何工作的。 如果我使用 Visual Stud
这是我的 JSON: {"data":[{"id":1,"layoutLabel":"Sameer Non Custom","orbNumber":"["0","1","2","3"]"}, {"id
我想反序列化包含关卡信息的 json 文件。给定这个名为 1.json 的示例 .json 文件 { "name": "Level One", "map": [ [{ "g
我一直在学习 this 的教程视频。 教程源代码可以在here找到... 我有一个问题,我的 Json 数据都存储在 1 行中, 我希望数据在单个对象数据中换行 这里是 Json 教程的代码 Acto
我有一些数据需要序列化/反序列化,但 JsonUtility 只是没有做它应该做的事情。这是我正在使用的对象: public class SpriteData { public string
这个问题在这里已经有了答案: Serialize and Deserialize Json and Json Array in Unity (9 个回答) 关闭 5 年前。 我在将 json 字符串
我有以下类,它是可序列化的,并且只有字符串作为字段: using UnityEngine; using System.Collections; using System.Collections.Gen
我从 Servlet 获得了一个 JSON 字符串,我想使用 JavaScript 覆盖类型解析这个字符串。 我的整个编码都基于您发布的示例 google我的目标是将 JSON 字符串加载到 Arti
我正在使用 RequestBuilder 读取静态 JSON 文件,并确认我在响应的 .getText() 方法中看到了预期的 JSON。我已经精简了我正在做的事情来尝试找出确切的问题。这就是我能做的
我试图了解如何使用 JSONutil 在 jquery 和 Coldfusion 之间序列化/反序列化 JSON。我无法使用 Coldfusion 7,因此无法在 cfc 中使用 returnform
模型 using System.Collections.Generic; using UnityEngine; [System.Serializable] public class GetPeople
这是我在 Unity 中发出的请求: UnityWebRequest request = UnityWebRequest.Put(baseUrl + "miniGame1s/", JsonUtilit
我是一名优秀的程序员,十分优秀!