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.
Related
I'm querying a mySQL (MariaDB) database and use JSON_ARRAY and JSON_OBJECT to gather json objects as array.
I have an api to retrieve the data from the DB, by using GetAsync.
Afterwards, in the api consumer, I want to parse the Json into an object,
using JArray (Newtonsoft).
I can't manage to parse the Json due to problem in the JSON_OBJECT (it seems like a quote wrapping the array sign "["). See error code below.
What am I doing wrong and how can I fix it ?
When getting another Json from another data source (not using JSON_ARRAY and JSON_OBJECT), parsing is working (see example below)
This is my code:
public JARrray parse(HttpResponseMessage response)
{
return JArray.Parse(response.Content.ReadAsStringAsync().Result);
}
This is the string that works:
[
{
"remark":"",
"driver": [
{
"username":"0000000",
"uid":"00000000",
"fname": "000000",
"lname": ""
}
]
}
]
And this is the string that get the message
After parsing a value an unexpected character was encountered: u. Path '[0].driver', line..."
I can see in the stack that this is happening on the line:
JsonTextReader.ParsePostValue(Boolean ignoreComments):
Json:
[
{
"remark":"",
"driver": "[{\"username\": \"0000000\", \"uid\": \"00000000\", \"fname\":\"ZZZZZ\", \"lname\":\"\"}]"
}
]
or even, when I removing the "" :
[
{
"remark":"",
"driver": "[{"username": "0000000", "uid": "00000000", "fname":"ZZZZZ", "lname":""}]"
}
]
\"fname\":"\ZZZZZ\"
The escaped double quote in front of ZZZZZ is wrong. Change it from "\ to \".
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
I have the below json as a text, how can i assign this text into a string? Since this has got multiple inverted commas in it. Escasping each comma becomes a tedious job. I need to assign the text to string json=""; and pass a player pref string in unity. For a later purpose , so that i can later on write the string by using System.IO.File.WriteAllText(Application.persistentDataPath + "/stickers.json", json);
{
"android_play_store_link": "adityaspt",
"ios_app_store_link": "",
"sticker_packs": [
{
"identifier": "1",
"name": "Adi",
"publisher": "Jane Doe",
"tray_image_file": "Trayicon_Cat1.png",
"image_data_version":"1",
"avoid_cache":false,
"publisher_email":"",
"publisher_website": "",
"privacy_policy_website": "",
"license_agreement_website": "",
"stickers": [
{
"image_file": "Formidable.webp",
"emojis": ["☕","🙂"]
},
{
"image_file": "Awful.webp",
"emojis": ["😩","😰"]
},
{
"image_file": "Athletic.webp",
"emojis": ["☕","🙂"]
}
]
}
]
}
As answered here you can use # in front of a string to automatically escape characters that usually cause issues. However you need to double up on your quotes for obvious reasons.
For example:
string quote = #"This is an example quote where someone could say ""Hello, World!"" without having to escape using \."
Which will output (backslash included):
This is an example quote where someone could say "Hello, World!"
without having to escape using .
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.
I have been trying to use Google Transliterate API using the RESTful approach as its easy to do so through server side language (C# here).
So, I came across this URL format: http://www.google.com/transliterate/indic?tlqt=1&langpair=en|hi&text=bharat%2Cindia&tl_app=3 which returns the JSON in the format:
[
{
"ew" : "bharat",
"hws" : [
"भारत","भरत","भरात","भारात","बहरत",
]
},
{
"ew" : "india",
"hws" : [
"इंडिया","इन्डिया","इण्डिया","ईन्डिया","इनडिया",
]
},
]
I tried HttpWebRequest and HttpWebResponse to get the JSON but it returned values in Unicode on the web browser, such as:
[ { "ew" : "bharat", "hws" : [ "\u092D\u093E\u0930\u0924","\u092D\u0930\u0924","\u092D\u0930\u093E\u0924","\u092D\u093E\u0930\u093E\u0924","\u092C\u0939\u0930\u0924", ] }, { "ew" : "india", "hws" : [ "\u0907\u0902\u0921\u093F\u092F\u093E","\u0907\u0928\u094D\u0921\u093F\u092F\u093E","\u0907\u0923\u094D\u0921\u093F\u092F\u093E","\u0908\u0928\u094D\u0921\u093F\u092F\u093E","\u0907\u0928\u0921\u093F\u092F\u093E", ] }, ]
So, I applied this article and passed the JSON string via it, and it returned:
[ { "ew" : "bharat", "hws" : [ "भारत","भरत","भरात","भारात","बहरत", ] }, { "ew" : "india", "hws" : [ "इंडिया","इन्डिया","इण्डिया","ईन्डिया","इनडिया", ] }, ]
FIRST QUESTION: Am I doing it right so far? Because in the browser it DOES NOT show the last " ] ", however " ] " exists in the HTML source (not sure why that happened). Also, when I try to parse it, using (I might be wrong using this technique):
var jss = new JavaScriptSerializer();
var dict = jss.Deserialize<Dictionary<string, dynamic>>(the_JSON_string);
Its giving me error saying:
Invalid array passed in, extra trailing ','.
SECOND QUESTION: If I am doing right so far, can I get some help parsing the Hindi words? What approach should I take using preferably System.Web.Script.Serialization;. Eventually I want to grab the Hindi text for further processing.
Please help, thanks.
I would recommend Json.Net to parse json strings. Below code(with your sample string) works and you don't need to do anything to unescape those characters. Json parsers will handle it for you.
string json = #"[ { ""ew"" : ""bharat"", ""hws"" : [ ""\u092D\u093E\u0930\u0924"",""\u092D\u0930\u0924"",""\u092D\u0930\u093E\u0924"",""\u092D\u093E\u0930\u093E\u0924"",""\u092C\u0939\u0930\u0924"", ] }, { ""ew"" : ""india"", ""hws"" : [ ""\u0907\u0902\u0921\u093F\u092F\u093E"",""\u0907\u0928\u094D\u0921\u093F\u092F\u093E"",""\u0907\u0923\u094D\u0921\u093F\u092F\u093E"",""\u0908\u0928\u094D\u0921\u093F\u092F\u093E"",""\u0907\u0928\u0921\u093F\u092F\u093E"", ] }, ]";
dynamic obj = JsonConvert.DeserializeObject(json);
MessageBox.Show(obj[0].hws[0].ToString());
i think you can remove last comma like a below way
the_JSON_string = the_JSON_string.Remove(the_JSON_string.LastIndexOf(','));