String Manipulation in C#: deserialize JSON - c#

Can I use a string function in C# to manipulate this string;
"[{\"param\":\"Update Payment\",\"value\":\"50.00\"}]"
I want to get at all times the string in position "Update Payment" as well as the value in "50.00"

Since this appear to be JSON why don't you use a JSON deserializer ? http://james.newtonking.com/pages/json-net.aspx for example.

Related

C# Pass filename as json parameter- Getting error "Unrecognized escape sequence. "

I want to pass a filepath through JSON. On deserializing I am getting error:
Unrecognized escape sequence. (43): {"Jobtype": "StepBatch","SelectedId": "D:\Input\file1.CATPart"}
I have escaped characters but it still shows error...am I missing something here?
string json = "{\"Jobtype\": \"StepBatch\",\"SelectedId\": \"D:\\Input\\file1.CATPart\"}";
var jsonObj = new JavaScriptSerializer().Deserialize<List<Arguments>>(json);
The problem is that the content of your string at execution time is:
{"Jobtype": "StepBatch","SelectedId": "D:\Input\file1.CATPart"}
That's not valid JSON, because of the backslashes in the value for SelectedId. You'd need the JSON to be:
{"Jobtype": "StepBatch","SelectedId": "D:\\Input\\file1.CATPart"}
so your C# would have to be:
string json = "{\"Jobtype\": \"StepBatch\",\"SelectedId\": \"D:\\\\Input\\\\file1.CATPart\"}";
However, given that you're immediately deserializing the JSON anyway, I'd suggest getting rid of the JSON part entirely, and just creating the Arguments values yourself.
If you need to produce JSON, create the right values directly, and then get JavaScriptSerializer (or preferrably Json.NET) to create the JSON for you, instead of hand-coding it.

I have a json value in database ["20200"] in this format

,i need just 20200 value, how can I take this value? when try to take to a string its return a value like "[\"20200"]"
The squared brackets denote an array of values. You can use a JSON serialization library like NewtonSoft JSON to deserialize the data into an IEnumerable<string> and then take the first value:
var v = JsonConvert.DeserializeObject<IEnumerable<string>>("[\"20200\"]").FirstOrDefault();

Can I Deserialize a JSON string that contains 0.0 in C#?

The JSON I'm getting back from a webservice has an integer incorrectly represented as 0.0. My deserialization code looks like this:
var serializer = new JsonSerializer();
var ret = serializer.Deserialize<T>(jsonTextReader);
And I get an error like this:
Input string '0.0' is not a valid integer.
My question is, is there a way to specify a less strict deserialization method so that I can parse this string?
EDIT: The web service returns no schema so I don't know why the deserializer tries to convert it to an int instead of a float or double.
I'd say that you should go ahead and creat your classes on Json -> C#
var o = (JObject)serializer.Deserialize(myjsondata);
You can use the C# dynamic type to make things easier. This technique also makes re-factoring simpler as it does not rely on magic-strings. Use JsonConvert.DeserializeObject<dynamic>()to deserialize this string into a dynamic type then simply access its properties in the usual way in C#.
Im not sure why youre getting
Input string '0.0' is not a valid integer.
since if you dont have any Json data it should just be left at null and you shouldnt have this problem

How to create json schema from json object string C#

I am evaluating Json.Net.Schema from NewtonSoft and NJsonSchema from GitHub and I cannot figure out how to create a JSON schema from a JSON object. I want it to work exactly like this site does: http://jsonschema.net/#/
What I am looking for
string json = #"{""Name"": ""Bill"",""Age"": 51,""IsTall"": true}";
var jsonSchemaRepresentation = GetSchemaFromJsonObject(json);
I would expect a valid JSON schema in the jsonSchemaRepresentation variable. Does anyone know how I can accomplish this?
Thanks in advance!
The current version of NJsonSchema supports this feature:
The SampleJsonSchemaGenerator generates a JSON Schema from sample JSON data.
var schema = JsonSchema4.FromSampleJson("...");
var schemaJson = schema.ToJson();
... or create a SampleJsonSchemaGenerator instance and call the Generate("...") method.
Actually both of the libraries you mentioned do not support such a functionality.
If you're down to implement it yourself then you will have to parse your JSON, iterate over it recursively and add a new schema depending on the type of what you've just iterated over.
There are also some other tools (in other languages like python) which could be an inspiration, this might get you started.
The string you are submitting to the function is not in the correct format. Try this (add '{' to the start of the string, '}' to the end):
string json = #"{
""Name"": ""Bill"",
""Age"": 51,
""IsTall"": true
}";
var jsonSchemaRepresentation = GetSchemaFromJsonObject(json);

Is it possible to get a value out of a literal string in c#?

I have a literal string that contains details of a json array that i need to extract a value from in C#
The string looks like the following:
"{\"Field1\":[],\"Field2\":333,\"Field3\":\"string\"....
Now Field2 is the field i wish to get in this isntance, but i have no idea how to in C#
Check out the Newtonsoft.Json package on nuget.org, it can parse the JSON for you and then you can retrieve the keys by name
Since the value is in JSON format, use JSON.Net to deserialize it to form a C# type then you can read the value as you read any other property in a class
Another way (apart from using external packages/addons whatever) would be writing a small regex-function like this:
public string GetField(string fieldName)
{
Regex rgxGetField = new Regex(fieldName + "\\\":(.*?),");
Match mGetField = rgxGetField(jString);
return = mGetField.Groups[1].Value;
}
For sure only works for the format you posted in your question.
You want to deserialize the JSON string. See How to Deserialize JSON data? for a number of excellent answers.
thanks for all the help
i ended up using the following:
dynamic d = JObject.Parse(string);
field2= d.field2;

Categories