使用Json.NET将JSON转换为DataSet [英] JSON to DataSet using Json.NET

查看:1634
本文介绍了使用Json.NET将JSON转换为DataSet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望有人可以伸出援助之手...我正在尝试使用以下示例将JSON转换为数据集,但遇到了问题.我已经验证了JSON的正确性,并使用了Kent建议的方法.感谢您的时间和帮助!

hoping someone can lend a hand... I am trying to convert JSON to a DataSet, using the below example, but am having problems. I've validated that the JSON is correct, used a method as suggested by Kent. Thanks for your time and help!

以下是我的JSON:

{"jsonData":[{"item1":"one"},{"item2":"two"}]}

这是我的Web服务C#代码:

[WebMethod]
        public string setWorkOrdersUpdated(object jsonData)
        {
            try
            {
                XmlDocument xd = new XmlDocument();
                xd = (XmlDocument)JsonConvert.DeserializeXmlNode(jsonData.ToString());
                DataSet ds = new DataSet();
                ds.ReadXml(new XmlNodeReader(xd));
                return "success";
            }
            catch (Exception e)
            {
                return "ERROR: " + e + "!";
            }
        }

这是我的错误输出之一: d:错误:Newtonsoft.Json.JsonReaderException:解析值:S. Path",第0行,位置0 ...时遇到意外字符."

Here's one of my error outputs: d: "ERROR: Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: S. Path '', line 0, position 0..."

推荐答案

您使用的是哪个版本?因为我遇到了另一个错误(Json.net 4.5).我已经完成了以下操作,并且异常消失了(基本上传递了根元素名称).

Which version are you using? since I was getting a different error (Json.net 4.5). I have done the following and the exception has gone (basically passing the root element name).

修改:添加了完整的代码

string jsonData = "{\"jsonData\":[{\"item1\":\"one\"},{\"item2\":\"two\"}]}";
XmlDocument xd = new XmlDocument();
//xd = (XmlDocument)JsonConvert.DeserializeXmlNode(jsonData.ToString()); //Throws exception "This document already has a 'DocumentElement' node."
xd = (XmlDocument)JsonConvert.DeserializeXmlNode(jsonData.ToString(), "jsonData");
DataSet ds = new DataSet();
ds.ReadXml(new XmlNodeReader(xd));

如果这不能解决您的问题,请告诉我.

Please let me know if this is not solving your problem.

这篇关于使用Json.NET将JSON转换为DataSet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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