,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();
Related
Dears
I have a byte array that is returned from web server , it is a part of json-serialized object (property value)
It looks like below in the json string:
,"n":"y1GpP7FibyTYl40Jhx1B90WOi1mecJfpi4IEhbHPbAB64jhV16UlpEPyGpNIzDS4Lct80sIs7FW5Vnf38Z-tzPbtHyFVYYU2AC4SVrwQp9-ELz-..._xW3bmMxuwoBgHpWDTw"
Please note that there is no double equal sign at the end, like for Base64 strings. I've used three dots (...) to make string representation a little bit shorter
I can deserialize object and get proper byte array:
var kb = JsonConvert.DeserializeObject<KeyBundle>(Properties.Resources.keyBundleJson);
And can it serialize to json back:
JsonSerializerSettings settings = new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.None,
Formatting = Formatting.Indented
};
string json = JsonConvert.SerializeObject(kb, settings);
But the problem is that result property value looks not the same as original string:
from web server it was:
y1GpP7FibyTYl40Jhx1B90WOi1mecJfpi4IEhbHPbAB64jhV16UlpEPyGpNIzDS4Lct80sIs7FW5Vnf38Z-tzPbtHyFVYYU2AC4SVrwQp9-ELz-..._xW3bmMxuwoBgHpWDTw
serialized locally:
y1GpP7FibyTYl40Jhx1B90WOi1mecJfpi4IEhbHPbAB64jhV16UlpEPyGpNIzDS4Lct80sIs7FW5Vnf38Z+tzPbtHyFVYYU2AC4SVrwQp9+ELz+.../xW3bmMxuwoBgHpWDTw==
underscores and slashes, plus and minus signs, two equal signs at the end
is it possible to serialize byte array exactly as it is done by web-server?
I have an idea to serialize it with Json and then replace minus with plus, underscore with slash and remove last two equal signs.
Any other method to get it immediately out of the box?
Regards
In urls there is different variant of Base64 used with - and _ which doesn't require additional encoding (e.g. + would be encoded to %2B). For this you can simply use string Replace method to replace those characters.
If you want an out-of-the box solution you can try Microsoft.IdentityModel.Tokens nuget package:
var encoded = Base64UrlEncoder.Encode(someString);
var decoded = Base64UrlEncoder.Decode(encoded);
For more info: https://en.wikipedia.org/wiki/Base64#URL_applications
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
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;
JavaScriptSerializer oSerializer = new JavaScriptSerializer();
object i = 3;
string sJSON = oSerializer.Serialize(i); //"3"
The JavaScriptSerializer should serialize its parameter to JSON!
And the result is "3" ( which is not JSON)
What am I missing?
edit
Ive written a mail to douglas crockford
3 is not a json object/text but json value.
so i think msdn should clarify the serialize method.
http://i.stack.imgur.com/VOh3X.png
As has been said many times by different people, the output you are receiving is valid JSON.
From the JSON Specification (the Introduction):
JSON can represent four primitive types (strings, numbers, booleans, and null) and two structured types (objects and arrays).
and further (Section 2.1):
A JSON value MUST be an object, array, number, or string, or one of the following three literal names:
false null true
My interpretation of specification tells me that the case you describe here is more a JSON value than a JSON object.
You asked it to serialise the value 3, and it did. That's exactly correct.
To be explicit: what exactly are you expecting to come out? JSON gives name-value pairs. The value "3" has no name, because the whole object is 3.
JSON is JavaScript object notation. Pass it an object, and you'll probably get what you're expecting.
You can use an anonymous type as M. Babcock suggests: new { i = 3 }.
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.