使用 LitJson 在 C# 中反序列化 JSON 对象数组

发布于 2024-10-16 19:55:59 字数 1511 浏览 2 评论 0原文

我受到 Unity3d 3 新安全措施的限制,无法将 LitJson 用于 Web 播放器应用程序,否则它将无法编译。

有没有一种简单的方法将此输出序列化为对象(userEcoGet.text 的内容):

{"building_id":"1","building_level":"0","workers":"0","building_health":"100"}{"building_id":"2","building_level":"0","workers":"0","building_health":"100"}...{"building_id":"32","building_level":"0","workers":"0","building_health":"100"}

此代码仅输入 JSON 中的第一个对象:

using UnityEngine;
using System.Collections;
using LitJson;
...
public EcoData localEcoData;
...
localEcoData = JsonMapper.ToObject<EcoData>(userEcoGet.text);
...
public class EcoData
{
    public string building_id;
    public string building_level;
    public string workers;
    public string building_health;
}

将 localEcoData 转换为数组会导致缺少构造函数错误。我只是不知道在这种情况下如何正确嵌套构造函数,甚至不确定它是否有帮助。

此时,我求助于使用 JsonReader 并手动填充对象。任何帮助将不胜感激。谢谢。

编辑: 这太疯狂了,今天一整天都被杀了,只是因为我给自己造成了蹩脚的 JSON 格式...

现在工作正常,localEcoData[1,2...32] 是一个对象数组,其中所有元素都可访问:

public EcoData[] localEcoData;
...
localEcoData = JsonMapper.ToObject<EcoData[]>(userEcoGet.text);

我拥有的一切要做的就是在处理 MySql 的 PHP 页面上正确编写 JSON 并以有序的方式传输回 Unity。我是直接回显,没有使用数组进行传输。现在输出看起来像这样并且效果很好:

[{"building_id":"1","building_level":"0","workers":"0","building_health":"100"}{"building_id":"2","building_level":"0","workers":"0","building_health":"100"}...{"building_id":"32","building_level":"0","workers":"0","building_health":"100"}]

谢谢,伙计!

I'm limited by Unity3d 3 new security measures to using LitJson for Web Player application, otherwise it won't compile.

Is there a simple way to serialize this output as an object (contents of userEcoGet.text):

{"building_id":"1","building_level":"0","workers":"0","building_health":"100"}{"building_id":"2","building_level":"0","workers":"0","building_health":"100"}...{"building_id":"32","building_level":"0","workers":"0","building_health":"100"}

This code only enters first object from JSON:

using UnityEngine;
using System.Collections;
using LitJson;
...
public EcoData localEcoData;
...
localEcoData = JsonMapper.ToObject<EcoData>(userEcoGet.text);
...
public class EcoData
{
    public string building_id;
    public string building_level;
    public string workers;
    public string building_health;
}

Turning localEcoData into array leads to missing constructor errors. And I just don't know how to nest constructor correctly in this situation, not even sure if it would help even.

At this point I'm resorting to using JsonReader and filling in object manually. Any help would be appreciated. Thank you.

Edit:
That's just crazy, killed all day today just because of a crappy JSON formatting that I inflicted on myself...

This works perfectly now, localEcoData[1,2...32] is an object array with all elements accessible:

public EcoData[] localEcoData;
...
localEcoData = JsonMapper.ToObject<EcoData[]>(userEcoGet.text);

All I had to do is compose JSON correctly on PHP page which handles MySql and transfer back to Unity in orderly fashion. I was echoing directly without using an array for transfer. Now output looks like this and works great:

[{"building_id":"1","building_level":"0","workers":"0","building_health":"100"}{"building_id":"2","building_level":"0","workers":"0","building_health":"100"}...{"building_id":"32","building_level":"0","workers":"0","building_health":"100"}]

Thanks, man!

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑叹一世浮沉 2024-10-23 19:55:59

我怀疑问题出在你的 JSON 本身。它的格式不正确。要将其解析为 JSON 数组,您需要采用以下格式:

{
    result:[
                {"building_id":"1","building_level":"0","workers":"0","building_health":"100"},
                {"building_id":"2","building_level":"0","workers":"0","building_health":"100"},...
                {"building_id":"32","building_level":"0","workers":"0","building_health":"100"}
           ]
}

如果您无法控制 JSON,那么您可能需要拆分字符串并将每个字符串视为单个 JSON 对象,然后将数组中的每个元素转换为 C#对象类型。

I suspect the problem is with your JSON itself. its not in correct format. To parse it as JSON Array you will need to have it in format:

{
    result:[
                {"building_id":"1","building_level":"0","workers":"0","building_health":"100"},
                {"building_id":"2","building_level":"0","workers":"0","building_health":"100"},...
                {"building_id":"32","building_level":"0","workers":"0","building_health":"100"}
           ]
}

If you dont have control on the JSON then you may need to split the string and treat each string as a single JSON object and Convert each element in the array to you C# object type.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文