如何使用JsonUtility(unity c#)用任意字符串键反序列化json? [英] how to deserialize json with arbitrary string keys using JsonUtility(unity c#)?

查看:80
本文介绍了如何使用JsonUtility(unity c#)用任意字符串键反序列化json?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Unity,c#,使用JsonUtility.说我有一个json字符串,如下所示:

Unity, c#, use JsonUtility. Say I have a json string as follows:

{
    "1,1":"dd",
    "2,1":"abc",
    "2,2":"123"
}

键的数量和内容是任意的.我该如何反序列化此json并使用JsonUtility.FromJson<>()转移到我自己的类?

The amount and content of keys are arbitrary. How can I deserialize this json and transfer to my own class using JsonUtility.FromJson<>()?

如果键是固定的,那么我知道您可以使用带有键名称的变量来创建一个类.如果键是任意键怎么办?

If the keys are fixed, then I know you can make a class with variables with the name of the keys. What to do if keys are arbitrary?

推荐答案

尚未找到实现此目的的方法.解决方法:更改JSON结构以避免使用带有任意键的字典.问题中提到的字典可以更改为:

Still haven't found a way to do this. Workaround: change the JSON structure to avoid dictionary with arbitrary keys. The dictionary mentioned in the question can be changed into:

[
    {"key":"1,1","value":"dd"},
    {"key":"2,1","value":"abc"},
    {"key":"2,2","value":"123"}
]

这篇关于如何使用JsonUtility(unity c#)用任意字符串键反序列化json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆