返回介绍

JsonUtility.FromJson 来自Json文本

发布于 2019-12-18 15:37:54 字数 3087 浏览 1457 评论 0 收藏 0

JavaScript => public static function FromJson(json: string): T;
JavaScript =>public static function FromJson(json: string, type: Type): object;
C# => public static T FromJson(string json);
C# =>public static object FromJson(string json, Type type);

Parameters 参数

jsonThe JSON representation of the object.
typeThe type of object represented by the JSON.

Returns 返回

T An instance of the object.

Description 描述

Create an object from its JSON representation.

从Json表达式创建对象。

Internally, this method uses the Unity serializer; therefore the type you are creating must be supported by the serializer. It must be a plain class/struct marked with the Serializable attribute. Fields of the object must have types supported by the serializer. Fields that have unsupported types, as well as private fields or fields marked with the NonSerialized attribute, will be ignored.

内部工具,该方法使用Unity序列化;因此你创建的类型必须支持序列化。它必须是有序列化属性标记的类或结构。对象的域必须支持序列化。不支持的域和私有域或者非序列化属性标记的域一样,将会被忽略。

Only plain classes and structures are supported; classes derived from UnityEngine.Object (such as MonoBehaviour or ScriptableObject) are not.

仅支持普通的类和结构;从UnityEngine.Object派生的类(例如MonoBehaviour 或者ScriptableObject)是不支持的。

If the JSON representation is missing any fields, they will be given their default values (i.e. a field of type T will have value default(T) - it will not be given any value specified as a field initializer, as the constructor for the object is not executed during deserialization).

如果JSON表示法丢失任何部分,它们将会指定默认值(即T类型域会是默认值-它将不会指定任何值初始化,对象结构在反序列化期间不会执行)。

These methods can be called from background threads.

该方法可以被底层线程调用。

JavaScript:

#pragma strict
@Serializable
public class PlayerInfo {
	public var name: String;
	public var lives: int;
	public var health: float;
	public static function CreateFromJSON(jsonString: String) {
		return JsonUtility.FromJson.<PlayerInfo>(jsonString);
	}
	// name == "Dr Charles", lives == 3, and health == 0.8f.
}

C#:

using UnityEngine;
 
[Serializable]
public class PlayerInfo
{
	public string name;
	public int lives;
	public float health;
 
	public static PlayerInfo CreateFromJSON(string jsonString)
	{
		return JsonUtility.FromJson<PlayerInfo>(jsonString);
	}
 
	// Given JSON input:
	// {"name":"Dr Charles","lives":3,"health":0.8}
	// this example will return a PlayerInfo object with
	// name == "Dr Charles", lives == 3, and health == 0.8f.
 
}

jsonutility

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文