Reputation: 53
I am consuming an api from a .NET project and I am getting a json response, but I can't parse it correctly using Newtonsoft.Json.
I want to get the values like eda50ef6a96442088e88401ffb4846df
or 965507aad38245b1b7cc62a397c9af2e
. I also checked this json with an online json validator and it says it's wrong. What should I do? Any help?
{
"resp": {
"state": "ok",
"Query": {
"Header": {
"Cell": {
"HeaderName": "Guid",
"Type": "System.Guid"
}
},
"Row": {
"Cell": {
"name": "Guid",
"eda50ef6a96442088e88401ffb4846df"
}
},
"Row": {
"Cell": {
"name": "Guid",
"965507aad38245b1b7cc62a397c9af2e"
}
}
}
}
}
I also put here the response of the service in xml format.
<resp state="ok">
<Query>
<Header>
<Cell>
<HeaderName>Guid</HeaderName>
<Type>System.Guid</Type>
</Cell>
</Header>
<Row>
<Cell name="Guid">eda50ef6a96442088e88401ffb4846df</Cell>
</Row>
<Row>
<Cell name="Guid">965507aad38245b1b7cc62a397c9af2e</Cell>
</Row>
</Query>
</resp>
Upvotes: 0
Views: 261
Reputation: 129657
As others have already pointed out, the "JSON" being produced by this API is definitely invalid as it does not conform to the JSON standard.
I want to address the second part of your question, "What should I do?"
Obviously, if you control the project or API which is producing this JSON, then you should fix it to make it produce valid JSON. Instead of hand-coding the output (which is most likely how this output came to be) you should instead build up objects in memory (using nested dictionaries and lists would be a good choice here) and then serialize them using a proper JSON serializer like Json.Net. The serializer will be able to turn them into correct JSON for you.
If you do NOT control the sproject or API which is producing this JSON, then you should definitely notify the project author that it is producing invalid output and ask them to fix it, if possible.
If the project owner cannot be contacted, or s/he can't or won't fix it, or if you don't have time to wait for a proper fix, then you should switch to using the XML version instead. This looks to be correctly formatted and you should be able to consume this without any trouble using standard XML classes like LINQ-to-XML.
If you absolutely must have JSON to work with, for whatever reason, then you can try using Json.Net to convert the XML response directly into JSON using JsonConvert.SerializeXNode
:
string json = JsonConvert.SerializeXNode(XElement.Parse(xml), Formatting.Indented);
However, there are some potential pitfalls with this approach, notably that the conversion process can produce different JSON depending on the number of nodes in the XML. See Converting between JSON and XML for more information.
With the XML shown in your question, SerializeXNode
would produce this JSON:
{
"resp": {
"@state": "ok",
"Query": {
"Header": {
"Cell": {
"HeaderName": "Guid",
"Type": "System.Guid"
}
},
"Row": [
{
"Cell": {
"@name": "Guid",
"#text": "eda50ef6a96442088e88401ffb4846df"
}
},
{
"Cell": {
"@name": "Guid",
"#text": "965507aad38245b1b7cc62a397c9af2e"
}
}
]
}
}
}
Upvotes: 2
Reputation: 226
It looks like the JSON is malformed to me... particularly here:
"Row": {
"Cell": {
"name": "Guid",
"eda50ef6a96442088e88401ffb4846df"
}
Note that the string you want to get has no "Key" name. So the Cell Key has a value which is a JSON object with a name Key (whose value is "Guid"), but the long string has no key name.
It should have a key for that value, something like this:
"Row": {
"Cell": {
"name": "Guid",
"value": "eda50ef6a96442088e88401ffb4846df"
}
For the JSON to be valid, it needs to consist of key/value pairs.
Upvotes: 0