AABB
AlignmentBehavior
ArriveBehavior
AStar
BFS
BoundingSphere
BVH
BVHNode
Cell
CellSpacePartitioning
CohesionBehavior
CompositeGoal
ConvexHull
Corridor
CostTable
DFS
Dijkstra
Edge
EntityManager
EvadeBehavior
EventDispatcher
Behavior
FollowPathBehavior
FuzzyAND
FuzzyCompositeTerm
FuzzyFAIRLY
FuzzyModule
FuzzyOR
FuzzyRule
FuzzySet
FuzzyTerm
FuzzyVariable
FuzzyVERY
GameEntity
Goal
GoalEvaluator
Graph
GraphUtils
HalfEdge
HeuristicPolicyDijkstra
HeuristicPolicyEuclid
HeuristicPolicyEuclidSquared
HeuristicPolicyManhattan
InterposeBehavior
LeftSCurveFuzzySet
LeftShoulderFuzzySet
LineSegment
Logger
MathUtils
Matrix3
Matrix4
MemoryRecord
MemorySystem
MeshGeometry
MessageDispatcher
MovingEntity
NavEdge
NavMesh
NavMeshLoader
NavNode
Node
NormalDistFuzzySet
OBB
ObstacleAvoidanceBehavior
OffsetPursuitBehavior
OnPathBehavior
Path
Plane
Polygon
Polyhedron
PriorityQueue
PursuitBehavior
Quaternion
Ray
RectangleTriggerRegion
Regular
RightSCurveFuzzySet
RightShoulderFuzzySet
SAT
SeekBehavior
SeparationBehavior
SingletonFuzzySet
Smoother
SphericalTriggerRegion
State
StateMachine
SteeringBehavior
SteeringManager
Task
TaskQueue
Telegram
Think
Time
TriangularFuzzySet
Trigger
TriggerRegion
Vector3
Vehicle
Version
WanderBehavior

toJSON

介绍

toJSON 是 Yuka 库中的一个方法,可以将对象序列化为 JSON 格式的字符串。该方法是 Telegram 使用的对应方法,并提供中文技术文档。

用法

toJSON 方法需要在一个对象上调用,返回一个 JSON 格式的字符串。例如:

const myObject = {
    name: 'John',
    age: 30,
};
const jsonString = toJSON(myObject);
console.log(jsonString); // 输出 '{"name":"John","age":30}'

参数

toJSON 方法不接收任何参数。

返回值

toJSON 方法返回一个包含传入对象属性的 JSON 格式字符串。如果该对象都没有属性,则返回一个空对象 {} 的字符串。

异常处理

如果要序列化的对象不是有效的 JSON 数据,则该方法将抛出 TypeError 异常。

示例

const myObject = {
    name: 'John',
    age: 30,
};
const jsonString = toJSON(myObject);
console.log(jsonString); // 输出 '{"name":"John","age":30}'

const emptyObject = {};
const emptyJsonString = toJSON(emptyObject);
console.log(emptyJsonString); // 输出 '{}'

const invalidObject = {func: function(){}};
try {
    const invalidJsonString = toJSON(invalidObject);
} catch (error) {
    console.error(error.message); // 输出 'Converting circular structure to JSON'
}

支持的版本

toJSON 方法从 Yuka 版本 2.1.0 开始支持,并在此后的版本中不断进行改进。