Y is my object being serialized in a weird manner while using newtonsoft.json from ASP.Net Web API?
var s = JsonConvert.SerializeObject(request, Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
Output string as below,
"{\"head\":{\"version\":\"1.0\",\"serial\":\"20140102,6,125\",\"skinId\":\"Test\""
I want to send Json format string to a third party rest service (they accept only Json format).
Any help is much appreciated.
Cheers
S
There's nothing to be worried about here. This is the correct serialized object json. It is appearing like this because you are viewing this in visual studio by hovering variable to view its value, because in c# \" is used to represent a " in a string. When you will write this value in a text file(just to test actual value) , you will see what is it's actual value as:
string json="{\"head\":{\"version\":\"1.0\",\"serial\":\"20140102,6,125\",\"skinId\":\"Test\"";
File.WriteAllText("c:\\tests on.txt",json) ;
You will see the json in file what you actually want.
It's because you have serialized it twice, can you post more of your code or skip calling SerializeObject completely
I doubt you are still seeking an answer, but my workaround was to create a JObject with Newtonsoft and pass that.
Either:
JObject jBytes = Object.Parse(JsonConvert.SerializeObject(myObject, MyDateTimeFmtString);
or
JObject jBytes = JObject.FromObject(myObject, MyJsonSerializer);
The first case was my second choice, but I think there is a bug in Newtonsoft where JObject.FromObject ignores the DateFormatString in JsonSerializer.
Related
I got some trouble with a problem when use Newtonsoft json.net to deserialize json string to dictionary. It 's a case of my json string have some special character.
string jsonString = "{\"name\":\"Jones Smith\",\"age\":\"20\",\"description\":\"The one live with \"ALIGATOR\"\"}";
Dictionary<string, object> dict = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonString);
I try to find a solution in the use of json.net but i not found. So the FINAL plan is remove that "characters". So, what is the best solution for this case?
I think you can't do very much in your situation besides changing the format at the origin. The problem with your input is that there are " characters escaped the same way once in your json directly and once in your json values.
Consider the following part: "description":"The one live with "ALIGATOR""
How should a deserializer know which " should be considered part of the value or part of the json format?
I got the answer, like the last comment, that 's not valid JSON, below is valid JSON
{"name":"Jones Smith","age":"20","description":"The one live with \"ALIGATOR\""}
And all i can do is add '\' before special characters if the value of field description is "The one live with "ALIGATOR"" to make a valid JSON and convert to c# like this:
string jsonString = {\"name\":\"Jones Smith\",\"age\":\"20\",\"description\":\"The one live with \\"ALIGATOR\\"\"}
I'm somehow having troubles deserializing a json string into a simple List or string[] (I don't care which).
As of what I know, this is how to do this job:
JsonConvert.DeserializeObject<List<string>>(jsonString);
Here I am getting a RuntimeBinderException. It complains about the parameter, although my json string is valid and simple: a:1:{i:0;s:10:"Sahibinden";}
What you have isn't JSON is a serialized PHP object. There have been some tools that work well with this in C# but there isn't native support. If you own the PHP, then convert the object/array to JSON first. If not try the information on this question: https://stackoverflow.com/a/1923626/474702
Your JSON is invalid. Problems:
a:1 should be inside an object bracket of {}
The : before the { is invalid, you need a , there
The ; just after i:0 is invalid, you need a comma there
You repeat the mistake described in 1. and 2. inside your {} brackets as well
Solution: You need to read about JSON and make sure you understand its syntax.
I'm trying to figure out how to prettify when the JavaScriptSerializer serializes my custom object to json.
string json = _jsonSerializer.Serialize(listToSerializeToJson);
it is just one long string right now, not formatted.
I don't see an option for that on the built-in JavaScriptSerializer class. However, if you're using Json.Net you can do this.
string json=JsonConvert.Serialize(listToSerializeToJson, Formatting.Indented);
According to Json.Net's project page, JavaScriptSerializer and DataContractJsonSerializer do not support creating indented prettified JSON strings.
If it's just for testing/human readability, I like using Json Lint: http://jsonlint.com/.
Is there any way to handle newlines in JSON.NET. I have some data coming back with Carriage Return Line Feed in it and Json.Net is just leaving it raw in the return value. Is there a way to force Json.Net to encode this for Json. I assumed this would happen by default but it is not happening for me. Maybe I am missing something else.
I am using Json.Net in a MVC4 WebApi project if that matters.
My data is coming back with \r\n in the string such as
"Keywords": "These are my keywords.\r\n\r\n\r\nThis is a second line...\r\n\r\nThis is a third line. ...\r\n\r\n\r\nThis is a 4th line ..."
From what I understand, that should be
\\r\\n. It could be a problem with the data I am returning, but I just wanted to see what JSON.NET should be doing with this.
For me, the problem was that even if the serialized Json object looked correct in the debugger, when I write it to file it gets all these literals added (i.e. backslashes are added).
What worked was to parse the obtained json as a Token, and then use the token instead.
For example:
// Serialize and convert to Token
string json = Newtonsoft.Json.JsonConvert.SerializeObject(someObject);
var token = JToken.Parse(json);
// Save to file
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(filepath, token);
I honestly do not understand why this would be needed, or if there is a better way around this. Feedback in comment is appreciated.
You can serialize your object with the option Formatting.Indented.
Like this:
string yourJsonString = JsonConvert.SerializeObject(yourObject, Formatting.Indented, new JsonSerializerSettings { });
I think this should work.
Regards.
I'd like to be able to parse a string of JSON representing an object into a property bag (like a Dictionary) which I can use in C#.
Given this string:
{ "id":1, "name":"some name", "some parameter":2 }
I want to end up with a Dictionary which contains "id", "name", and "some parameter" as keys and 1, "some name", and 2 as values respectively.
I don't want to parse the JSON string myself - maybe there's a library (preferably in the .net framework) that I can lean on to do the parsing for me to give access to the key/values in the JSON object. Or is there a deserializer available which I can explicitly tell which .net type to target?
In my scenario I'll only ever have one root "object" (it won't start with an array).
Thanks.
var json = new JavaScriptSerializer() { MaxJsonLength = int.MaxValue };
var dict = (IDictionary<string, object>)json.DeserializeObject(yourString);
JavaScriptSerializer should do what you need.
Also, Json.NET if you don't mind going 3rd party.
Have you looked at the DataContractJsonSerializer and JavaScriptSerializer to see if either of those meet your needs?
If not, you could also try JSON.NET