【问题标题】:Unity JsonUtility can't reach nested variable [duplicate]Unity JsonUtility无法访问嵌套变量[重复]
【发布时间】:2018-01-26 20:55:11
【问题描述】:

这是我用于测试目的的 JSON 文件:

{"timestamp":4455518515,"profileId":"5asd15qwe11as2d12q1sd","profileName":"player01",
"textures":{"material":{"url":"http://materialurl.com/aksjd54854we5a4sd.png"}}}

我想访问纹理 -> 材料 -> url,但我做不到。这是我的代码:

public class JsonTest : MonoBehaviour {

string json;
string path;

// Use this for initialization
void Start () {
    path = Application.streamingAssetsPath + "/json.txt";
    json = File.ReadAllText(path);

    MaterialJson test = JsonUtility.FromJson<MaterialJson>(json);

    Debug.Log(test.timestamp);
    Debug.Log(test.profileId);
    Debug.Log(test.profileName);
    Debug.Log(test.textures);
    }
}

[Serializable]
public class MaterialJson
{
    public double timestamp;
    public string profileId;
    public string profileName;
    public string textures;
}

前三个变量没有问题。但是textures 在调试日志中返回 null。我不知道如何在textures 中获取嵌套变量。 unity的JsonUtility 有能力吗?我找不到关于统一文档的任何信息。

它与下面的代码完美配合:(感谢@mrfreester)

[Serializable]
public class MaterialJson
{
    public double timestamp;
    public string profileId;
    public string profileName;
    public Textures textures;
}
[Serializable]
public class Textures
{
    public Material material;
}

[Serializable]
public class Material
{
    public string url;
}

Debug.Log(test.textures.material.url);

【问题讨论】:

  • 纹理不是字符串而是 JSON 对象
  • Debug.Log(test.textures.material); 是什么意思?
  • 这不是我有大量经验的领域......但看起来你期待textures成为string这里......public string textures.. .但是它是你的json中的一个对象。您可能需要创建一个 Serializable 类 textures,其中包含一个 material 类,如果您希望它与您的 json 一起使用,它包含一个 public string url
  • @Quintium 是的,但我也不知道如何解析该对象。
  • @mrfreester 哦,它已使用此代码 Debug.Log(test.texture.material.url);十分感谢你的帮助。你可以把它写成一个解决方案

标签: c# json unity3d deserialization


【解决方案1】:

看起来你希望纹理在这里是一个字符串

public string textures;

但是它是您的 json 中的一个对象。

如果您希望每个关联的 json 对象以当前格式与您的 json 一起工作,您可以为每个关联的 json 对象创建一个 Serializable 类。如您的评论所述:

[Serializable] 
public class MaterialJson 
{ 
    public double timestamp; 
    public string profileId; 
    public string profileName; 
    Textures texture; 
} 

[Serializable] 
public class Textures 
{ 
    Material material; 
} 

[Serializable] 
public class Material 
{ 
    public string url; 
}

然后会像这样访问:

Debug.Log(test.texture.material.url);

【讨论】:

    【解决方案2】:

    方法一:

    你可以用这个:

    using (var ms = new System.IO.MemoryStream(Encoding.Unicode.GetBytes(json)))
    {
       System.Runtime.Serialization.Json.DataContractJsonSerializer deserializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(MaterialJson));
       MaterialJson test = (MaterialJson)deserializer.ReadObject(ms);
    }
    

    方法二:

    感谢NewtonsoftJSON.Net,您可以试试这个:

    var test =  JsonConvert.DeserializeObject<MaterialJson>(json);
    

    MaterialJson 类如下:

    [Serializable]
    public class MaterialJson
    {
        public double timestamp;
        public string profileId;
        public string profileName;
        public texturesType textures;
        public class texturesType
        {
            public materialType material;
            public class materialType
            {
                public string url;
            }
        }
    }
    

    现在您可以使用test.textures.material.url 访问url 属性

    【讨论】:

    • 感谢您的评论,但实际上我不想使用任何其他库,因为我会在移动平台上发布它,我不知道它是否有效
    • @sword1st 我已经更新了答案。请再次检查
    • 如果嵌套类不会在其他地方使用,则为 +1。对于统一内置在 JasonUtility 中,它们可能还必须标有 [Serializable]
    猜你喜欢
    • 2022-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-24
    • 2017-07-06
    相关资源
    最近更新 更多