使用DataContractJsonSerialiser解析json文件
我有使用TexturePacker 导出的json 文件,它会生成这种格式。
{"frames": {
"But_01_Highlight.png":
{
"frame": {"x":0,"y":0,"w":280,"h":41},
"rotated": false,
"trimmed": true,
"spriteSourceSize": {"x":7,"y":8,"w":280,"h":41},
"sourceSize": {"w":294,"h":57}
},
我正在遵循一个教程,您可以将其转换为数组,但这在 Windows Phone 7 上不可用。感觉就像我对每种格式进行了逆向工程,而不是仅按解析时读取它。
我如何创建一个带有数据契约的对象来加载这种格式?
我的问题也类似于以下问题 https://stackoverflow.com/questions/3769322/datacontractjsonserializer -with-任意密钥名称 没有答案
@Andreas Löw,如果你可以导出到这样的格式,那就太好了。
{"frames":[
{
"filename": "But_01_Highlight.png",
"frame": {"x":0,"y":0,"w":280,"h":41},
"rotated": false,
"trimmed": true,
"spriteSourceSize": {"x":7,"y":8,"w":280,"h":41},
"sourceSize": {"w":294,"h":57}
},
...
]
I have json file that I have exported using TexturePacker, and it produces this format.
{"frames": {
"But_01_Highlight.png":
{
"frame": {"x":0,"y":0,"w":280,"h":41},
"rotated": false,
"trimmed": true,
"spriteSourceSize": {"x":7,"y":8,"w":280,"h":41},
"sourceSize": {"w":294,"h":57}
},
I was following a tutorial where you could just turn it into an array but that's not available on windows phone 7. It feels like I have the reverse engineer every format rather than just read it as is parsed.
How would I create an object with a datacontract to load this format?
My question is also similar to the following question https://stackoverflow.com/questions/3769322/datacontractjsonserializer-with-arbitrary-key-names which has no answer
@Andreas Löw if you could export to a format like so it would be great.
{"frames":[
{
"filename": "But_01_Highlight.png",
"frame": {"x":0,"y":0,"w":280,"h":41},
"rotated": false,
"trimmed": true,
"spriteSourceSize": {"x":7,"y":8,"w":280,"h":41},
"sourceSize": {"w":294,"h":57}
},
...
]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该工具位于 http://carlosfigueira.me/JsonUtilities/JsonToContract.htm(在博客中描述)发布http://blogs.msdn.com /b/carlosfigueira/archive/2011/01/11/inferring-schemas-for-json.aspx)可用于创建对象图,该对象图可用于使用 DataContractJsonSerializer 反序列化该 JSON。这是该工具的输出(由于该工具中的错误,我不得不将类“But_01_Highlight.png”的名称更改为“But_01_Highlight_png”)。
此外,考虑到所有 JSON 数据都遵循相同的“架构”,这也是可行的。如果情况并非如此,那么 DataContractJsonSerializer 并不是最佳选择。对于 WP7,您可以使用一些 JSON 库,例如 System.Json 命名空间(您需要从 Silverlight 3.0 SDK 添加对 System.Json.dll 的引用)
The tool at http://carlosfigueira.me/JsonUtilities/JsonToContract.htm (described in the blog post http://blogs.msdn.com/b/carlosfigueira/archive/2011/01/11/inferring-schemas-for-json.aspx) can be used to create an object graph which can be used to deserialize that JSON using the DataContractJsonSerializer. This is the output of the tool (I had to change the name of the class "But_01_Highlight.png" to "But_01_Highlight_png" because of a bug in the tool).
Also, this works given that all the JSON data follows the same "schema". If this is not the case, then the DataContractJsonSerializer is not the best option for that. For WP7, you can use some JSON library such as the classes on the System.Json namespace (you need to add a reference to the System.Json.dll from the Silverlight 3.0 SDK)