All, I know this question has been asked multiple times, and I feel like I have looked at every single one of them on here! I have a C# Winforms application that is making a call to an outside webhook, pulling in JSON formatted data. I need to take this data and convert it to a data table, with the end result imported into a database. The thing I am stuck on, and have been for the last day and a half, is trying to parse the JSON data into XML.
Here is the JSON data (example) I am getting from the webhook:
[
["Item Title",
"Bidder Name",
"Bidder Email",
"Bidder Phone Number",
"Bidder Username",
"Bid Amount",
"Bid Time",
"Operation",
"Auto Bid Amount",
"Bidder Address",
"Bidder City",
"Bidder State",
"Bidder Country"
],
["Test item 1(#)"],
["",
"Tom Kelly",
"[email protected]",
"6303278300",
"testaccount",
"50.0",
"07/09/2016 07:17 PM CDT",
"Bid Amount",
null,
null,
null,
null,
"US"
]
]
At first I tried using the DeserializeXMLNode function, but that didn't work.
XmlNode xml = JsonConvert.DeserializeXmlNode(body, "BiddingHistory");
Then I thought that I would use the DeserializeObject function, but again, not working.
var jRst = JsonConvert.DeserializeObject(body);
When I use the DeserializeObject function I am getting the following result:
{[
[
"Item Title",
"Bidder Name",
"Bidder Email",
"Bidder Phone Number",
"Bidder Username",
"Bid Amount",
"Bid Time",
"Operation",
"Auto Bid Amount",
"Bidder Address",
"Bidder City",
"Bidder State",
"Bidder Country"
],
[
"Test Item 1"
],
[
"",
"Tom Kelly",
"[email protected]",
"6303278300",
"testaccount",
"75.0",
"07/30/2016 06:14 PM CDT",
"Bid Amount",
null,
null,
null,
null,
"US"
]
]}
After converting the object to a string, I ran this through the XMLNodeConverter, and it was failing on the "#" character, so I did a string replace and took out that character (wasn't needed anyway and I do not have control of what is coming to me in the JSON data) then ran my code again.
XmlNode xml = JsonConvert.DeserializeXmlNode(sBody, "BiddingHistory");
So now what I am getting is this error:
XmlNodeConverter can only convert JSON that begins with an object
Would someone please steer me in the right direction here? I'm thinking this is a simple thing to do and I am overcomplicating it.
Thanks.
You are getting an error because your JSON data is an array and what you have done is:
XmlNode xml = JsonConvert.DeserializeXmlNode(sBody, "BiddingHistory");
the above line of code will only work for JSON objects.
So, if your JSON is an Array, then try this:
XmlNode xml = JsonConvert.DeserializeXmlNode("{\"Row\":" + sBody + "}", "BiddingHistory").ToXmlString();
Use service stack from nuget Service Stack
add reference to your program
using ServiceStack;
Convert your json to object
var jRst = JsonConvert.DeserializeObject(body);
after that you can get xml using service stack like below
var xml = jRst.ToXml();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With