如何更改json数据格式 [英] How to change json data format
本文介绍了如何更改json数据格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有特定格式的json数据,我需要更改我的代码中使用的数据格式。如何用c#代码或其他东西更改它?
我的尝试:
JSON
I have a json data in particular format,i need to change the data format to use in my code.How to change it in c# code or some other thing?
What I have tried:
JSON
[
"concepts": {
"http://dbpedia.org/resource/Chuckles_(G.I._Joe)": {
"SurfaceForms": [
{
"Score": 0.9460024,
"String": "CHUCKLES",
"Offset": 3
}
],
"Types": [ "" ],
"Support": 52
}
},
"concepts": {
"http://dbpedia.org/resource/Memory,_Sorrow,_and_Thorn": {
"SurfaceForms": [
{
"Score": 1.0,
"String": "Sithi",
"Offset": 2
}
],
"Types": [ "" ],
"Support": 43
}
}
]
在这个JSON数据中如何获得里面的分数一个SurfaceForms。
推荐答案
这篇文章将为您提供工具并告诉您如何做您要问的事:在C#中使用JSON& VB [ ^ ]
此外,您的JSON数据中存在错误。它应该是:
This article will give you the tools and show you how to do what you are asking: Working with JSON in C# & VB[^]
Also, there is an error in your JSON data. It should be:
{
"concepts": {
"http://dbpedia.org/resource/Chuckles_(G.I._Joe)": {
"SurfaceForms": [
{
"Score": 0.9460024,
"String": "CHUCKLES",
"Offset": 3
}
],
"Types": [ "" ],
"Support": 52
}
},
"concepts": {
"http://dbpedia.org/resource/Memory,_Sorrow,_and_Thorn": {
"SurfaceForms": [
{
"Score": 1.0,
"String": "Sithi",
"Offset": 2
}
],
"Types": [ "" ],
"Support": 43
}
}
}
这里是由 JSON Utils生成的C#类:生成C#,VB.Net,SQL表,Java和PHP JSON [ ^ ]:
And here is the C# classes generated by JSON Utils: Generate C#, VB.Net, SQL Table, Java and PHP from JSON[^]:
public class SurfaceForm
{
[JsonProperty("Score")]
public double Score { get; set; }
[JsonProperty("String")]
public string String { get; set; }
[JsonProperty("Offset")]
public int Offset { get; set; }
}
public class HttpDbpediaOrgResourceMemorySorrowAndThorn
{
[JsonProperty("SurfaceForms")]
public IList<SurfaceForm> SurfaceForms { get; set; }
[JsonProperty("Types")]
public IList<string> Types { get; set; }
[JsonProperty("Support")]
public int Support { get; set; }
}
public class Concepts
{
[JsonProperty("http://dbpedia.org/resource/Memory,_Sorrow,_and_Thorn")]
public HttpDbpediaOrgResourceMemorySorrowAndThorn HttpDbpediaOrgResourceMemorySorrowAndThorn { get; set; }
}
public class Result
{
[JsonProperty("concepts")]
public Concepts Concepts { get; set; }
}
To populate the classes, the above article link[^] will show you how.
您有两个选择:
1.创建一个适合JSON的类并使用它(更容易的方式恕我直言)
2.使用 JObject [来自Newtonsoft的 ^ ] - 它可以包含任何内容没有准备的JSON ......
You have two options:
1. Create a class that fits the JSON and use it (the easier way IMHO)
2. Use JObject[^] from Newtonsoft - it can contain any JSON without preparation...
这篇关于如何更改json数据格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文