The Json file I have got few bullet characters due to which it is not parsing. I tried different ways to replace the bullet characters but no success. Help please. Thanks.
Json file content is below
"items": [ { "type": 202, "path": "C\TestFile.json", "name": " • OptionA?" }]
I tried following with no success.
option 1:
System.IO.File.ReadAllText(#"C:\\Users\\xxx\\Files\\JsonTestFile.txt").Replace(#"\\u2022",string.Empty)
option 2:
System.Text.RegularExpressions.Regex.Replace(strFileContent, "[\\u2022,\\u2023,\\u25E6,\\u2043,\\u2219]\\s\\d", " ");
option 3: converted the file to UTF-8 in notepad++ and tried parsing but it still fails.
{"items": [ { "type": 202, "path": "C\TestFile.json", "name": " • OptionA?" }]}
Above is the correct json but it will not compile because of the value you have for "path".
You have to escape the backslash with another \.. Following deserializes correctly.
{ "items": [ { "type": 202, "path": "C:\\TestFile.json", "name": " • OptionA?" }] }
Console.WriteLine(JObject.Parse(json)["items"][0]["path"].ToString());
//prints
C:\\TestFile.Json
Related
I'm pulling intents back from API.AI and parsing these to C# objects using Newtonsoft.Json in the following way:-
intentListModel = JsonConvert.DeserializeObject<List<IntentListModel>>(intentList);
intentList is a JSON string from the webrequest. However at Line 1, position 161, it fails. The bit of JSON concerned is:-
"contextIn": [
"Employed"
],
"events": [{
"name": "Occupation_DOB"
}],
NB: This is only part of the JSON, and the JSON opens and closes with [] as it is a list of JSON items.
specifically the opening { on events. I'm stumped, I've run it through a validator and I see valid JSON.
Could anyone suggest what I can try, or is there a setting somewhere for this? Or is the error message actually looking another area of the JSON string?
Thanks in advance!
UPDATE
Whole JSON sample posted
[
{
"id":"18b025c5-3567-49c1-a9e9-25583f9156ca",
"name":"IncomeProtection - Employed? - Occupation/DOB/Email",
"state":"LOADED",
"contextIn":[
"Employed"
],
"events":[
{
"name":"Occupation_DOB"
}
],
"parentId":"ad5f0007-c084-4615-93dd-6c82ca5e7602",
"parameters":[
{
"required":true,
"dataType":"#Occupation",
"name":"Occupation",
"value":"$Occupation",
"prompts":[
"Whatu0027s your Occupation?"
],
"isList":false
},
{
"required":true,
"dataType":"#sys.date",
"name":"date",
"value":"$date",
"prompts":[
"Whatu0027s your date of birth?"
],
"isList":false
}
],
"contextOut":[
{
"name":"OccupationDOB",
"parameters":{
},
"lifespan":1
}
],
"actions":[
"IncomeProtection:Occupation/DOB"
],
"priority":500000,
"fallbackIntent":false
}
]
This issue was down to one of the items from the API being returned in a list, but in the particular example I looked at, the API returned a list of 1 item. I misread the brackets and created a class property of type string instead of List<string>, hence the failure of code.
Hope this helps people in the future.
This question already has answers here:
Census Geocoder JSON output convert to Xml dataset using JSON.net in C#
(2 answers)
Closed 5 years ago.
I have a simple json body
"widget": {
"debug": "on",
"window": {
"title": "Sample Konfabulator Widget",
"name": "main_window",
"width": 500,
"height": 500
},
"image": {
"src": "Images/Sun.png",
"name": "sun1",
"horizontal#Offset": 250,
"vOffset": 250,
"alignment": "center"
},
"text": {
"data": "Click Here",
"size": 36,
"style": "bold",
"name": "text1",
"hOffset": 250,
"vOffset": 100,
"alignment": "center",
"onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
}
}}
So during deserialization using
xmldoc = JsonConvert.DeserializeXmlNode(jsonBody, "widget")
I am getting this exception
The '#' character, hexadecimal value 0x40, cannot be included in a name.
My question is there any naming convention for NewtonSoft JSON parser, as it is not able to convert '#' character? If not, how to get rid of this exception?
As the error says # is not valid character in the xml element's name as per W3C https://www.w3.org/TR/2008/REC-xml-20081126/#NT-Name .You might have to change horizontal#Offset
Source JSON:
[
{
"$type": "Test.Contracts.TestClass, Test.Contracts",
"name": "name1",
"comments": [
{
"title": "Some value",
"text": "Some value \"in quotes\""
}
]
}
]
Deserialized string still contains escaped quotes:
Deserialization code:
JsonConvert.DeserializeObject<List<TestClass>>(json,
new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto
});
Is there any solution to the problem other than manually replacing \" symbols?
Visual Studio is escaping the quotes for the debugging view, they do not exist in the underlying string.
The easiest solution if you need to see the unquoted version is to press the magnifying glass.
I've a requirement like dragable tree in my project.
I've successfully converted mssql resultset to json.
But the end user is again dragging and dropping and totally changed the tree structure.
Now I have the below json from the client.
[
{
"id": 1,
"title": "1. dragon-breath",
"items": [
{
"id": 10,
"title": "1. dragon-breath.1",
"items": [
{
"id": 100,
"title": "1. dragon-breath.1.2",
"items": [
{
"id": 1000,
"title": "1. dragon-breath.1.2.3",
"items": [],
"pos": 3
}
],
"pos": 2
},
{
"id": 101,
"title": "1. dragon-breath.1.2",
"items": [],
"pos": 2
}
],
"pos": 1
}
],
"pos": 1
},
{
"id": 102,
"title": "1. dragon-breath.1.2",
"items": [
{
"id": 1020,
"title": "1. dragon-breath.1.2.1",
"items": [],
"pos": 1
}
],
"pos": 2
},
{
"id": 1021,
"title": "1. dragon-breath.1.2.1",
"items": [],
"pos": 1
}
]
From the above json "pos" property is for MSSQL Hierarchyid value.
I am trying to regenerate the hierarchy id of each element, when this json posted back from the client.
I am requesting you people to help me. (spend 2 days no luck)
Following the link you posted to the other SO question, and then a link from a comment, I see you are using the MSSQL hierarchyid data type (hint: it might have helped if you been a bit clearer about that in your question ;)
From the online help I see there is a Parse function, which takes a string.
So you need to traverse your JSON tree structure, building strings for each element in the form /1/1/3/ for example, and pass those back to your database along with the associated node id.
I have a json object and I want to get the value of :
entities > media > sizes > large > h
Is there a way to get it like XML -> Xpath method?
This is extra lines that is irrelevant to question just because of ...
{
"created_at": "Sun, 01 Jan 2012 17:05:32 +0000",
"entities": {
"user_mentions": [
{
"screen_name": "nurdanterbiyik",
"name": "nurdan",
"id": 264782080,
"id_str": "264782080",
"indices": [
0,
15
]
}
],
"media": [
{
"id": 153522253777219584,
"id_str": "153522253777219584",
"indices": [
44,
64
],
"media_url": "http://p.twimg.com/AiFrrSmCMAAdEID.jpg",
"media_url_https": "https://p.twimg.com/AiFrrSmCMAAdEID.jpg",
"url": "http://t.co/ZwHN9gvO",
"display_url": "pic.twitter.com/ZwHN9gvO",
"expanded_url": "http://twitter.com/emelkiraac/status/153522253773025280/photo/1",
"type": "photo",
"sizes": {
"large": {
"w": 536,
"h": 800,
"resize": "fit"
},
Using JSON.NET you have several ways to read data without having to deserialize your JSON text into objects. Here is a simplified example:
string json = #" {
""created_at"": ""Sun, 01 Jan 2012 17:05:32 +0000"",
""entities"": {
""media"": [{
""type"": ""photo"",
""sizes"": {
""large"": {
""w"": 536,
""h"": 800,
""resize"": ""fit""
}
}
}]
}
}
";
JObject o = JObject.Parse(json);
int h = (int)o["entities"]["media"][0]["sizes"]["large"]["h"];
int h2 = (int)o.SelectToken("entities.media[0].sizes.large.h");
I prefer to think of JSON purely as serialized objects--the first and last thing I want to do with it is to de-serialize it into the objects that I then work with.
I can't tell you the best way to do it in c#, but I've had good luck using JSON.NET
JSON.NET will also let you (quote from website) "LINQ to JSON is good for situations where you are only interested in getting values from JSON, you don't have a class to serialize or deserialize to, or the JSON is radically different from your class and you need to manually read and write from your objects. LINQ to JSON allows you to easily read, create and modify JSON in .NET.", which sounds like what you are trying to do. I've never tried it, but if all you're trying to do is get one piece of data out of that long string, then it sounds like a good alternative.