gpt4 book ai didi

Unity3d parsed Json file is printing Null values(Unity3D解析的Json文件正在打印空值)

转载 作者:bug小助手 更新时间:2023-10-28 10:56:09 26 4
gpt4 key购买 nike



My guess is, I am either having problems with my class structures or I am having problems with my json file itself. I've looked at plenty of examples of how to read the file, and I think that's okay. My problem is that I cannot print any data out of this structure. Either it prints zeros only, or I have an error attempting to print.

我的猜测是,要么是我的类结构有问题,要么是我的json文件本身有问题。我已经查看了大量如何读取文件的示例,我认为这没有问题。我的问题是我不能从这个结构打印出任何数据。要么它只打印零,要么我尝试打印时出错。


Here's a snippet of my json file.

以下是我的json文件的一个片段。


{
"0":{
"0":[
"56_Tele",
"Castle Spire",
0,
21
],
"1":[
"44_Reflecto",
"Did you remember?",
1,
18
],
"2":[
"55_ShipPiece",
"Salvation",
1,
21
],
"3":[
"36_Jump",
"Saving",
2,
11
]
}
}

And here's the class that reads in the json file:

下面是读入json文件的类:


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;

public class DataManager : MonoBehaviour
{
public TextAsset json_data;

//[System.Serializable]
public class WorldData
{
public uint WorldId;
public WorldSceneIds[] Scenes;
}

//[System.Serializable]
public struct WorldSceneIds
{
public uint SceneId { get; set; }
public WorldSceneData[] WorldSceneData { get; set; }
}

//[System.Serializable]
public class WorldSceneData
{
public string SceneName { get; set; }
public string LevelName { get; set; }
public uint MapXCoord { get; set; }
public uint MapYCoord { get; set; }
}

private void Start()
{

WorldData idstruct = JsonConvert.DeserializeObject<WorldData>(json_data.text);
Debug.Log(idstruct);
Debug.Log(idstruct.Scenes[0].SceneId);
}

}


I've tried changing the json file and the structures I am reading it into. I tried reading different fields from the structure that I've loaded in, all are malformed.

我已经尝试更改了json文件和我正在将其读入的结构。我试着从我加载的结构中读取不同的字段,都是畸形的。


更多回答

Classes should be serializable and only use fields. You can use properties adorned with [field:SerializeField] but each field name will be prepended with k__BackingField in the JSON.

类应该是可序列化的,并且只使用字段。您可以使用用[field:SerializeField]装饰的属性,但在JSON中,每个字段名都将以k__BackingField作为前缀。

Also, keep in mind that these classes will be serialized by value which means that you will lose the by reference nature of classes.

此外,请记住,这些类将按值序列化,这意味着您将失去类的按引用性质。

Last, haven't tested your code, but there appears to be some kind of cyclic reference between IDs and DATAs, the serializer is kinda primitive and might choke on these, i.e. better review your data structures.

最后,还没有测试代码,但是ID和数据之间似乎有某种循环引用,序列化程序有点原始,可能会被这些东西卡住,也就是说,最好检查一下数据结构。

优秀答案推荐

you have to convert your array to a class

您必须将数组转换为类


    WorldData[] idstruct = JObject.Parse(json)
.Properties()
.Select(o => new WorldData
{
WorldId = Convert.ToUInt32(o.Name),
Scenes = ((JObject)o.Value)
.Properties()
.Select(i => new JObject
{
["SceneId"] = Convert.ToUInt32(i.Name),
["WorldSceneData"] =
new JObject(
i.Value.Select((i, v) => new JProperty(v.ToString(), i)))
}.ToObject<WorldSceneIds>())
.ToArray()
}).ToArray();

//test
uint sceneId = idstruct[0].Scenes[0].SceneId;

and fix the classes

并修复这些类


public class WorldData
{
public uint WorldId { get; set; }
public WorldSceneIds[] { get; set; }
}

public class WorldSceneIds
{
public uint SceneId { get; set; }
public WorldSceneData WorldSceneData { get; set; }
}

public class WorldSceneData
{
[JsonProperty("0")]
public string SceneName { get; set; }
[JsonProperty("1")]
public string LevelName { get; set; }
[JsonProperty("2")]
public uint MapXCoord { get; set; }
[JsonProperty("3")]
public uint MapYCoord { get; set; }
}

or using dictionaries

或使用词典


    Dictionary<string, Dictionary<string, WorldSceneData>> idstructDict = JObject
.Parse(json)
.Properties()
.ToDictionary(o => o.Name, o => ((JObject)o.Value).Properties()
.ToDictionary(v => v.Name, i => new JObject(
i.Value
.Select((v, i) => new JProperty(i.ToString(), v)))
.ToObject<WorldSceneData>()));

//tests

sceneId = Convert.ToUInt32(idstructDict["0"].Keys.ToArray()[1]); // "1"

var sceneName = idstructDict["0"].Values.ToArray()[0].SceneName; // "56_Tele"

更多回答

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com