- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我有一个使用 WWW
从 PHP 文件发送到 unity 的项目列表.WWW.text
看起来像:
[
{
"playerId": "1",
"playerLoc": "Powai"
},
{
"playerId": "2",
"playerLoc": "Andheri"
},
{
"playerId": "3",
"playerLoc": "Churchgate"
}
]
[]
来自
string
.当我尝试使用
Boomlagoon.JSON
解析它时,只检索第一个对象。我发现我必须
deserialize()
列表并已导入 MiniJSON。
deserialize()
这份 list 。我想遍历每个 JSON 对象并检索数据。如何使用 C# 在 Unity 中执行此操作?
public class player
{
public string playerId { get; set; }
public string playerLoc { get; set; }
public string playerNick { get; set; }
}
[]
我能够使用 MiniJSON 解析 json。但它只返回第一个
KeyValuePair
.
IDictionary<string, object> players = Json.Deserialize(serviceData) as IDictionary<string, object>;
foreach (KeyValuePair<string, object> kvp in players)
{
Debug.Log(string.Format("Key = {0}, Value = {1}", kvp.Key, kvp.Value));
}
最佳答案
统一添加 JsonUtility到他们的 API 之后5.3.3 更新。除非您正在做一些更复杂的事情,否则请忘记所有 3rd 方库。 JsonUtility 比其他 Json 库更快。更新到 Unity 5.3.3 版本或更高版本然后尝试下面的解决方案。JsonUtility
是一个轻量级的 API。仅支持简单类型。确实如此 不是 支持字典等集合。一个异常(exception)是 List
.支持List
和 List
大批!
如果你需要序列化一个 Dictionary
或者做一些不是简单地序列化和反序列化简单数据类型的事情,使用第三方 API。否则,继续阅读。
要序列化的示例类:
[Serializable]
public class Player
{
public string playerId;
public string playerLoc;
public string playerNick;
}
public static string ToJson(object obj);
序列化为 Json方法。
Player playerInstance = new Player();
playerInstance.playerId = "8484239823";
playerInstance.playerLoc = "Powai";
playerInstance.playerNick = "Random Nick";
//Convert to JSON
string playerToJson = JsonUtility.ToJson(playerInstance);
Debug.Log(playerToJson);
{"playerId":"8484239823","playerLoc":"Powai","playerNick":"Random Nick"}
public static string ToJson(object obj, bool prettyPrint);
序列化为 Json方法重载。简单路过
true
到
JsonUtility.ToJson
函数将格式化数据。将下面的输出与上面的输出进行比较。
Player playerInstance = new Player();
playerInstance.playerId = "8484239823";
playerInstance.playerLoc = "Powai";
playerInstance.playerNick = "Random Nick";
//Convert to JSON
string playerToJson = JsonUtility.ToJson(playerInstance, true);
Debug.Log(playerToJson);
{
"playerId": "8484239823",
"playerLoc": "Powai",
"playerNick": "Random Nick"
}
public static T FromJson(string json);
反序列化 json方法重载。
string jsonString = "{\"playerId\":\"8484239823\",\"playerLoc\":\"Powai\",\"playerNick\":\"Random Nick\"}";
Player player = JsonUtility.FromJson<Player>(jsonString);
Debug.Log(player.playerLoc);
public static object FromJson(string json, Type type);
反序列化 json方法重载。
string jsonString = "{\"playerId\":\"8484239823\",\"playerLoc\":\"Powai\",\"playerNick\":\"Random Nick\"}";
Player player = (Player)JsonUtility.FromJson(jsonString, typeof(Player));
Debug.Log(player.playerLoc);
public static void FromJsonOverwrite(string json, object objectToOverwrite);
反序列化 json方法。当
JsonUtility.FromJsonOverwrite
使用时,不会创建您要反序列化的对象的新实例。它只会重用您传入的实例并覆盖其值。
Player playerInstance;
void Start()
{
//Must create instance once
playerInstance = new Player();
deserialize();
}
void deserialize()
{
string jsonString = "{\"playerId\":\"8484239823\",\"playerLoc\":\"Powai\",\"playerNick\":\"Random Nick\"}";
//Overwrite the values in the existing class instance "playerInstance". Less memory Allocation
JsonUtility.FromJsonOverwrite(jsonString, playerInstance);
Debug.Log(playerInstance.playerLoc);
}
playerId
出现超过
一次 . Unity的
JsonUtility
不支持数组,因为它仍然是新的,但您可以使用
helper从这个人的类得到
阵列 与
JsonUtility
合作.
JsonHelper
的类.直接从下面复制 JsonHelper。
public static class JsonHelper
{
public static T[] FromJson<T>(string json)
{
Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>>(json);
return wrapper.Items;
}
public static string ToJson<T>(T[] array)
{
Wrapper<T> wrapper = new Wrapper<T>();
wrapper.Items = array;
return JsonUtility.ToJson(wrapper);
}
public static string ToJson<T>(T[] array, bool prettyPrint)
{
Wrapper<T> wrapper = new Wrapper<T>();
wrapper.Items = array;
return JsonUtility.ToJson(wrapper, prettyPrint);
}
[Serializable]
private class Wrapper<T>
{
public T[] Items;
}
}
Player[] playerInstance = new Player[2];
playerInstance[0] = new Player();
playerInstance[0].playerId = "8484239823";
playerInstance[0].playerLoc = "Powai";
playerInstance[0].playerNick = "Random Nick";
playerInstance[1] = new Player();
playerInstance[1].playerId = "512343283";
playerInstance[1].playerLoc = "User2";
playerInstance[1].playerNick = "Rand Nick 2";
//Convert to JSON
string playerToJson = JsonHelper.ToJson(playerInstance, true);
Debug.Log(playerToJson);
{
"Items": [
{
"playerId": "8484239823",
"playerLoc": "Powai",
"playerNick": "Random Nick"
},
{
"playerId": "512343283",
"playerLoc": "User2",
"playerNick": "Rand Nick 2"
}
]
}
string jsonString = "{\r\n \"Items\": [\r\n {\r\n \"playerId\": \"8484239823\",\r\n \"playerLoc\": \"Powai\",\r\n \"playerNick\": \"Random Nick\"\r\n },\r\n {\r\n \"playerId\": \"512343283\",\r\n \"playerLoc\": \"User2\",\r\n \"playerNick\": \"Rand Nick 2\"\r\n }\r\n ]\r\n}";
Player[] player = JsonHelper.FromJson<Player>(jsonString);
Debug.Log(player[0].playerLoc);
Debug.Log(player[1].playerLoc);
Powai
User2
{"Items":
在接收到的字符串前面添加
}
在它的最后。
string fixJson(string value)
{
value = "{\"Items\":" + value + "}";
return value;
}
string jsonString = fixJson(yourJsonFromServer);
Player[] player = JsonHelper.FromJson<Player>(jsonString);
{
"USD" : {"15m" : 1740.01, "last" : 1740.01, "buy" : 1740.01, "sell" : 1744.74, "symbol" : "$"},
"ISK" : {"15m" : 179479.11, "last" : 179479.11, "buy" : 179479.11, "sell" : 179967, "symbol" : "kr"},
"NZD" : {"15m" : 2522.84, "last" : 2522.84, "buy" : 2522.84, "sell" : 2529.69, "symbol" : "$"}
}
JsonUtility
不支持这一点,因为“15m”属性以数字开头。类变量不能以整数开头。
SimpleJSON.cs
来自 Unity 的
wiki .
var N = JSON.Parse(yourJsonString);
string price = N["USD"]["15m"].Value;
Debug.Log(price);
var N = JSON.Parse(yourJsonString);
string price = N["ISK"]["15m"].Value;
Debug.Log(price);
var N = JSON.Parse(yourJsonString);
string price = N["NZD"]["15m"].Value;
Debug.Log(price);
JsonUtility.ToJson
序列化时出现的问题?
{}
获取空字符串或“
JsonUtility.ToJson
” ?
JsonHelper.ToJson
而不是
JsonUtility.ToJson
.
[Serializable]
到您正在序列化的类的顶部。
public string playerId { get; set; }
删除
{ get; set; }
. Unity 无法对此进行序列化。
JsonUtility.FromJson
反序列化时的问题?
Null
,确保 Json 不是 Json 数组。如果是,请使用上面的辅助类
JsonHelper.FromJson
而不是
JsonUtility.FromJson
.
NullReferenceException
反序列化时,添加
[Serializable]
到类(class)的顶端。
{ get; set; }
从每个变量中添加
[Serializable]
到每个类的顶部生成。
[{"playerId":"1","playerLoc":"Powai"},{"playerId":"2","playerLoc":"Andheri"},{"playerId":"3","playerLoc":"Churchgate"}]
{"Items":
在
前台那么它的
添加
}
在
结束 其中。
serviceData = "{\"Items\":" + serviceData + "}";
{"Items":[{"playerId":"1","playerLoc":"Powai"},{"playerId":"2","playerLoc":"Andheri"},{"playerId":"3","playerLoc":"Churchgate"}]}
public player[] playerInstance;
playerInstance = JsonHelper.FromJson<player>(serviceData);
playerInstance[0]
是你的第一个数据
playerInstance[1]
是你的第二个数据
playerInstance[2]
是你的第三个数据
playerInstance[0].playerLoc
,
playerInstance[1].playerLoc
,
playerInstance[2].playerLoc
......
playerInstance.Length
在访问之前检查长度。
{ get; set; }
来自
player
类。如果您有
{ get; set; }
,它不会工作。 Unity的
JsonUtility
不是 使用定义为
的类成员属性(property) .
关于c# - 在Unity中序列化和反序列化Json和Json数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36239705/
在撰写本文时,Unity 有一个名为 Unity LTS 2017.4.12f1 的版本,Unity 的最新版本是 2018.2.11 我知道 Unity LTS 应该是稳定版本,但这是否意味着 Un
我需要 Unity 来捕获所有按键,即使 Unity 没有焦点。 我尝试过使用: Input.KeyPress() 但这似乎只有在 Unity 拥有用户输入焦点的情况下才有效。我需要它在没有焦点时工作
我正在尝试统一实现一个TCP服务器。我正在使用 unity pro 3.5,当我在场景中运行此代码时,unity 挂起,完全没有响应,直到我用任务管理器杀死它。 using UnityEngine;
我想问一下,使用新的unity ads 2.0与unity ads相比,收入有什么区别 Unity Ads Unity Ads 2.0 最佳答案 Unity 广告支持 Unity 4.2.2 或更高版
当我运行我的应用程序时,我希望 Unity 打开两个窗口。 window 将有不同的摄像头,但两者都会看到同一个世界。 这样的事情可能吗? (我还没有找到任何证据表明这一点) 我知道我可以通过两个 U
我使用Unity Hub下载了最新的Unity编辑器,它对于编辑器、文档和语言包运行良好,但无法下载android构建支持。刚刚告诉我这两天下载失败很多次。 所以我从网页下载了UnitySetup-A
我使用Unity Hub下载了最新的Unity编辑器,它对于编辑器、文档和语言包运行良好,但无法下载android构建支持。刚刚告诉我这两天下载失败很多次。 所以我从网页下载了UnitySetup-A
我今天已将我的项目升级到 Prism 6.3.0 和 Unity 5.3.1。在此之前,我有 Prism 5 和 Unity 4。 现在我遇到了 Prism.Unity.UnityBootstrapp
Unity 中是否有与 StructureMap 中的 Registry 类等效的内容? 我喜欢考虑一个层/组件/库来自行配置它 - 从而填充容器。所以“父”层只需要知道注册类。 最佳答案 不,没有。
我似乎无法在任何地方找到 Microsoft.Practices.Unity.StaticFactory.dll。 还有其他注册静态工厂的方法吗? 寻找这样的东西 container.Register
是否可以统一尝试所有已定义的构造函数,从参数最多的构造函数到最不具体的构造函数(默认构造函数)? 编辑 我的意思是说: foreach (var constructor in concrete.Get
我有一个正在运行且运行良好的 Unity 应用程序,但我们目前正在通过对所有编译警告采取行动来清理我们的代码。 由于过时的 Microsoft.Practices.Unity.Configuratio
我正在使用 Visual Studio Code 在 Unity 中编写脚本。在“编辑”-“首选项”-“外部工具”-“外部脚本编辑器”下,我也选择了 VS Code。 打开脚本工作正常,但是当我尝试通
因此,我很确定这不是提出此类问题的正确论坛,因此我非常感谢有人将我链接到针对此问题的更好论坛(如果需要)。 我的问题: 在 Unity Hub 中,我进行了设置,以便 Unity 编辑器应下载到我的硬
问题:即使在 Cardboard 相机范围内,我也无法在任何地方看到我的 UI 文本。 截图: 我使用的是Unity 5.4.0b21版本,有人说可以通过降级到Unity 5.3版本来修复。 但是,我
我正在开发一个 Unity 项目,我正在使用 Google VR SDK for Unity 和 FirebaseMessaging.unitypackage 适用于 Unity 的 Firebase
好吧,在谷歌、这里和几个 ASP/MVC 论坛上搜索之后,我一定要问我到底做错了什么。 我的应用程序有一个良好的开端,对 DI、IoC 有很好的理解,并且正在使用 Repository、Service
我们最近将项目中的 Microsoft Unity 从 3.5.1404 版本升级到 5.8.6。在我们的代码中只做了一些小的调整,这次升级似乎很容易。它毫无问题地解决了我们所有注册的实例。但是,我们
我正在弄清楚使用 Unity 应用程序 block 时的意外行为。我有项目 A 作为我的启动项目。 项目 A 具有对项目 B 的项目引用,而项目 B 具有对项目 C 的项目引用。 项目 A 使用 un
我将 Unity 与 MVC 和 NHibernate 结合使用。不幸的是,我们的 UnitOfWork 驻留在不同的 .dll 中,并且它没有默认的空 .ctor。这是我注册 NHibernate
我是一名优秀的程序员,十分优秀!