C# 如何将 JSONString 解析为数据集?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19136024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to parse a JSONString To Dataset?
提问by Chirag Parmar
I am creating a C# Application using Web Services. In my Web Services I'm using a JSONString
data.
But I'm not able to convert this string into a DataSet
.
我正在使用 Web 服务创建 C# 应用程序。在我的 Web 服务中,我使用的是JSONString
数据。但我无法将此字符串转换为DataSet
.
My JSONString
is :
我的JSONString
是:
{
"Table": [
{
"DisplayVoucherNumber": "A101239Z",
"ActualDate": "08/07/2013",
"AccountName": "shyamal",
"Pcs": "50",
"Weight": "500.000"
}
],
"Table1": [
{
"DisplayVoucherNumber": "R101249B",
"ActualDate": "11/07/2013",
"AccountName": "vipul",
"NetWeight": "90.000",
"Weight": "80.000",
"Difference": "10.000"
},
{
"DisplayVoucherNumber": "R101249B",
"ActualDate": "11/07/2013",
"AccountName": "vipul",
"NetWeight": "500.000",
"Weight": "100.000",
"Difference": "400.000"
}
]
}
采纳答案by Oualid KTATA
Your question is not very clear. I guess that what you would like to do is get back an object that could be mapped to you data set after deserializtion. Something like
你的问题不是很清楚。我想您想要做的是取回一个可以在反序列化后映射到您的数据集的对象。就像是
DataSet myDataSet= JsonConvert.DeserializeObject<DataSet>(jsonstring)
And you keep going coding with you dataset. like accessing datatables inside the dataset.
你继续用你的数据集编码。比如访问数据集中的数据表。
If it's what you want to achieve and don't want to use your own POCO as suggested by previous answers. You might need to create a Typed DataSet before
如果这是您想要实现的目标并且不想按照以前的答案的建议使用您自己的 POCO。您可能需要先创建一个 Typed DataSet
Given an XML Schema that complies with the XML Schema definition language (XSD) standard, you can generate a strongly typed DataSet using the XSD.exe tool provided with the Windows Software Development Kit (SDK). More info on strongly typed Dataset
给定符合 XML 架构定义语言 (XSD) 标准的 XML 架构,您可以使用 Windows 软件开发工具包 (SDK) 提供的 XSD.exe 工具生成强类型数据集。 有关强类型数据集的更多信息
This will allow you to use the strongly typed dataset using the Deserialize method.
这将允许您使用 Deserialize 方法使用强类型数据集。
Bare in mind that you have to mimic your JSon Structure in the XML Schema. in order to have something compatible with your JSon Structure at the end.
请记住,您必须在 XML 模式中模仿您的 JSon 结构。为了最终与您的 JSon 结构兼容。
回答by Benyamin Maengkom
I prefer you follow Nick's suggestion... and perhaps your web services should modified because I saw similar property on each Table and Table1 and property inside...
我更喜欢你遵循尼克的建议......也许你的网络服务应该修改,因为我在每个 Table 和 Table1 上看到了类似的属性,并且里面的属性......
Create class using json2csharp.com and I got this
public class Table { public string DisplayVoucherNumber { get; set; } public string ActualDate { get; set; } public string AccountName { get; set; } public string NetWeight { get; set; } public string Weight { get; set; } public string Pcs { get; set; } public string Difference { get; set; } }
Deserialize json string using
List<Table> list = JsonConvert.DeserializeObject<List<Table>>(jsonstring);
- You can access that list object to put on your DataSet.
使用 json2csharp.com 创建类,我得到了这个
public class Table { public string DisplayVoucherNumber { get; set; } public string ActualDate { get; set; } public string AccountName { get; set; } public string NetWeight { get; set; } public string Weight { get; set; } public string Pcs { get; set; } public string Difference { get; set; } }
使用反序列化 json 字符串
List<Table> list = JsonConvert.DeserializeObject<List<Table>>(jsonstring);
- 您可以访问该列表对象以放置在您的数据集上。
回答by Maciej Jureczko
Create a classfor your deserialized data.
Use:
YourClass yourObject = JsonConvert.DeserializeObject<YourClass>(jsonStr);
为您的反序列化数据创建一个类。
使用:
YourClass yourObject = JsonConvert.DeserializeObject<YourClass>(jsonStr);
回答by Dhaval
Private Function convertJsonStringToDataSet(jsonString As String) As DataSet
Dim xd As New XmlDocument()
jsonString = "{ ""rootNode"": {" + jsonString.Trim().TrimStart("{"c).TrimEnd("}"c) + "} }"
xd = DirectCast(Newtonsoft.Json.JsonConvert.DeserializeXmlNode(jsonString), XmlDocument
Dim ds As New DataSet()
ds.ReadXml(New XmlNodeReader(xd))
Return ds
End Function
回答by shA.t
As a dynamicC# solution(when you don't know the object structure to deserialize) by using @Dhaval's answer and after invaliding Deserialize<>()
method I use below method to do that:
作为动态C# 解决方案(当您不知道要反序列化的对象结构时),通过使用@Dhaval 的答案并在使Deserialize<>()
方法无效后,我使用以下方法来做到这一点:
Update: DataSet.ReadXml
has some options in reading XML node as XmlReadMode
:
更新:DataSet.ReadXml
在读取 XML 节点时有一些选项XmlReadMode
:
private static DataSet ReadDataFromJson(string jsonString, XmlReadMode mode = XmlReadMode.Auto)
{
//// Note:Json convertor needs a json with one node as root
jsonString = $"{{ \"rootNode\": {{{jsonString.Trim().TrimStart('{').TrimEnd('}')}}} }}";
//// Now it is secure that we have always a Json with one node as root
var xd = JsonConvert.DeserializeXmlNode(jsonString);
//// DataSet is able to read from XML and return a proper DataSet
var result = new DataSet();
result.ReadXml(new XmlNodeReader(xd), mode);
return result;
}
E.g. If you want to infer a strongly typed schema from the data:
例如,如果您想从数据中推断出强类型模式:
var dataset = ReadDataFromJson(yourString, XmlReadMode.InferTypedSchema);