返回介绍

JsonUtility.ToJson 转为Json

发布于 2019-12-18 15:37:55 字数 3730 浏览 4856 评论 0 收藏 0

JavaScript => public static function ToJson(obj: object): string;
JavaScript =>public static function ToJson(obj: object, prettyPrint: bool): string;
C# => public static string ToJson(object obj);
C# =>public static string ToJson(object obj, bool prettyPrint);

Parameters 参数

objThe object to convert to JSON form.
prettyPrintIf true, format the output for readability. If false, format the output for minimum size. Default is false.

Returns 返回

string The object's data in JSON format.

Description 描述

Generate a JSON representation of the public fields of an object.

生成对象的公共域的Json标识法。

Internally, this method uses the Unity serializer; therefore the object you pass in must be supported by the serializer: it must be a MonoBehaviour, ScriptableObject, or plain class/struct with the Serializable attribute applied. The types of fields that you want to be included must be supported by the serializer; unsupported fields will be ignored, as will private fields, static fields, and fields with the NonSerialized attribute applied.

内部的,该方法使用Unity序列化器;因此你传递的对象必须支持序列化:它必须是MonoBehaviour,ScriptableObject或者简单的可序列化的class/struct。你希望重写的区域类型必须支持序列化;不支持区域将被忽略,例如私有域,静态域和非序列化属性标记的区域。

Any plain class or structure is supported, as well as classes derived from MonoBehaviour or ScriptableObject. Other engine types are not supported. (In the Editor only, you can use EditorJsonUtility.ToJson to serialize other engine types to JSON).

支持任何从MonoBehaviour 和ScriptableObject导出的普通类和结构。其他引擎类型是不支持的。仅在编辑器中你可以使用EditorJsonUtility.FromJsonOverwrite去重写其他引擎对象。

Note that while it is possible to pass primitive types to this method, the results may not be what you expect; instead of serializing them directly, the method will attempt to serialize their public instance fields, producing an empty object as a result. Similarly, passing an array to this method will not produce a JSON array containing each element, but an object containing the public fields of the array object itself (of which there are none). To serialize the actual content of an array or primitive type, it is necessary to wrap it in a class or struct.

注意通过该方法的原始类型,该结果可能不如预期;而不是立刻序列化它们,该方法将会尝试去序列化他们的公共实例化区域,因此产生一个空对象。

This method can be called from background threads. You should not alter the object that you pass to this function while it is still executing.

在方法可以被底层线程调用。当该函数运行使你不该修改或重写该对象。

JavaScript:

#pragma strict
public class PlayerState extends MonoBehaviour {
	public var playerName: String;
	public var lives: int;
	public var health: float;
	public function SaveToString() {
		return JsonUtility.ToJson(this);
	}
	// {"playerName":"Dr Charles","lives":3,"health":0.8}
}

C#:

using UnityEngine;
 
public class PlayerState : MonoBehaviour
{
	public string playerName;
	public int lives;
	public float health;
 
	public string SaveToString()
	{
		return JsonUtility.ToJson(this);
	}
 
	// Given:
	// playerName = "Dr Charles"
	// lives = 3
	// health = 0.8f
	// SaveToString returns:
	// {"playerName":"Dr Charles","lives":3,"health":0.8}
}

jsonutility

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

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

发布评论

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