C# 使用 LitJson 将 JsonData 对象转换为 JSON 字符串
您可以使用 LitJson 库中的 JsonUtility 类将 JsonData 对象转换为 JSON 字符串。以下是示例代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;
public class Example : MonoBehaviour
{
void Start()
{
// 创建 JsonData 对象
JsonData data = new JsonData();
data['name'] = 'Tom';
data['age'] = 20;
// 将 JsonData 对象转换为 JSON 字符串
string json = JsonUtility.ToJson(data);
Debug.Log(json); // 输出: {'name':'Tom','age':20}
}
}
此代码使用 LitJson 库中的 JsonData 类创建了一个实例,并使用 JsonUtility 类将其转换为 JSON 字符串。请注意,此方法仅适用于 JsonData 对象,对于其他对象类型,请使用其他方法进行转换。
原文地址: https://www.cveoy.top/t/topic/lMdh 著作权归作者所有。请勿转载和采集!