Parsing Unknown JSON using JavascriptSerializer in C# - c#

How can I use JavaScriptSerializer to parse some unknown dynamic JSON. In particular, I'm writing my own wrapper for the Google Calendar API. An event has an object called extendedProperties with both a private object and shared object containing an unknown set of properties:
"extendedProperties": {
"private": {
"UnknownKey1": "UnknownValue1",
"UnknownKey2": "UnknownValue2",
"UnknownKey3": "UnknownValue3"
},
"shared": {
"UnknownKey1": "UnknownValue1",
"UnknownKey2": "UnknownValue2",
"UnknownKey3": "UnknownValue3"
}
}
I want to create a class like this for the JavaScriptSerializer:
public class ExtendedProperties
{
public ??? #private { get; set; }
public ??? shared { get; set; }
}
Of course there are problems.
(1) Does the serializer understand the ampersand so it will parse the property 'private'?
(2) What would the return type be for the properties that the JavaScriptSerializer could read/write? Some sort of Dictionary?
Thanks in advance!

var serializer = new JavaScriptSerializer();
var jsonObject = serializer.Deserialize<IDictionary<string, object>>(jsonStr);
I have used this code to deserialize unknown json objects.

The parser understands the # symbol. You can use dynamic as your type if you're using .net 4. You could try Dictionary<string,string> although I've always had problems with serializing and deserializing dictionaries to the same reference object. List<KeyValuePair<string, string>> usually does the trick.

Related

Deserialize JSON object containing array to C# object

This is my JSON string:
{"type":"motor","ids":["1","2","5","7","8","10"]}
And this is the object I want to generate from it:
public class ElementArray {
public ElementType type;
public String[] ids;
public ElementArray() {
}
}
How can I achieve that? I googled about Json.NET, but they only explain how to deserialize an array but not how to deserialize an object containing an array as a field (see my class above).
What I tried is
JavaScriptSerializer jss = new JavaScriptSerializer();
ElementArray elements = jss.Deserialize<ElementArray>(strJson);
but when I debug the code, the field ids contains null.
IMO simplest way to deal with json is using newtonsoft.json library
http://www.newtonsoft.com/json
And here is example how to deserialize object:
http://www.newtonsoft.com/json/help/html/DeserializeObject.htm

C# Parse/Deserialize JSON partially with Newtonsoft

I have to extract a part of json-string using .net or newtonsoft json.
JSON:
var json = "{\"method\":\"subtract\",\"parameters\":{\"minuend\":\"SOME_CUSTOM_JSON_OBJECT_DIFFERENT_FOR_EACH_METHOD\",\"subtrahend\":23}}";
C# Class:
class MyJson{
public string method { get; set; }
//public string parameters {get; set;}
public object parameters {get; set;}
}
I do not need to parse all the children of "parameters" json-object. "parameters" could be a very big object ([{obj1}...{obj1000}], objX of 1000 fields), parse which would be not performant.
I would like i.e. to pass it exactly as it is on some point, so conversion "string-C#object-string" would be redundant.
I do not want use Regexp or string transformations (string.Substring, Split and co), because of error margin, I know that all .net and newtonsoft string transformations based.
Question 1: if I define a property of type "object", how newtonsoft will handle this? (Documentation is worse than msdn, so I'm looking for the input from you, who already tried this).
static void Main(string[] args)
{
var json = "{\"method\":\"subtract\",\"parameters\":{\"minuend\":42,\"subtrahend\":23}}";
var data = JsonConvert.DeserializeObject<MyJson>(j);
// what internal representaion of data.parameters?
// How is it actually converted from json-string to an C# object (JObject/JsonObject).
}
In perfect case:
"parameters" is a string and calling
ExtractMyJson(jsonString)
gives me the json string of parameters.
Basically I need the newtonsoft version of
string ExtractMyJson(jsonString){
var p1 = jsonString.Split(",");
// .. varios string transformations
return pParams;
}
Note: please don't reference "dynamic" keyword or ask why no string transformations, it's the very specific question.
If you know that your parameters are unique you can do something like this:
class MyJson
{
public string method { get; set; }
public Dictionary<string,object> parameters { get; set; }
}
................
string json = "{\"method\":\"subtract\",\"parameters\":{\"minuend\":{\"img\": 3, \"real\": 4},\"subtrahend\":23}}";
var data = JsonConvert.DeserializeObject<MyJson>(json);
If you let it as object is going to receive the type Newtonsoft.Json.Linq.JObject.
Have you tried JTOKEN?
It is a rather simple solution to partially read basic or nested JSONs as described in this post.
For a nested JSON
{
"key1": {
"key11": "value11",
"key12": "value12"
}
"key2": "value2"
}
it would look like this
JToken token = JToken.Parse(json);
var value12 = token.SelectToken("key1.key12");
to get the element of the key "key12.
I think this could go nicely with your problem.
Well Objects are treated the same way your parent object is treated. It will start from the base of the graph. So if you have something like:
Person
{
Address Address {get;set;}
}
The Json will start Deserializing Address and then add in the Person object.
If you want to limit thesize of the graph depth you can use a setting like :
JsonConvert.DeserializeObject<List<IList<IList<string>>>>(json, new JsonSerializerSettings
{
MaxDepth = 2
});
For more configurations of the JsonSerializer check JsonSerializerSettings
If your field is an object then that object will have the KeyValuePair of every property that it holds, based on that when you cast that field you can access that type.(the behaviour is the same as assigning a type to an object in C#).
Update: So if you question using JsonObject or type, well JObject is and intermediary way to construct the json format in a generic format. But using the Type deserializatin means you can ignore properties you are not interested in. Mapping to a json with a type makes more sense because it creates a new object and dismisses the old JObject.

Deserialize raw JSON and convert to custom C# Object

I have the following raw JSON string:
[\"Hello World!\",\"94952923696694934\",\"MyChannel\"]
I have tried the following without luck:
My custom object class:
public class MyObject
{
public string msg { get; set; }
public string id { get; set; }
public string chn { get; set; }
}
JSON string:
string str = "[\"Hello World!\",\"94952923696694934\",\"MyChannel\"]";
1st attempt at deserilization using System.Web.Script.Serialization:
JavaScriptSerializer serializer = new JavaScriptSerializer();
MyObject obj1 = serializer.Deserialize<MyObject>(str);
2nd attempt at deserilization using Newtonsoft.Json:
MyObject obj2 = JsonConvert.DeserializeObject<MyObject>(str);
Both attempts fail. Any suggestions?
You have a JSON array of strings, not an object with property names.
So the best you can do here is to deserialize the array:
IEnumerable<string> strings =
JsonConvert.DeserializeObject<IEnumerable<string>>(str);
...then use the resulting sequence strings as you see fit.
With PubNub, you can just pass in the native String, Dictionary, or Array, and we'll JSON encode it for you on the publish side, and auto JSON decode for you on the subscriber side.
It's because your 'custom object' isn't equivalent to the json representation. The json you're deserializing is just a string[] in C# (you can also use List<string> or other IEums).
So in code you're looking for;
string[] theJson = JsonConvert.DeserializeObject<string[]>(str);
MyObject would be used for the following json;
{
"msg":"Hello World!",
"id":"94952923696694934",
"chn":"MyChannel"
}

Deserializing Json with numbered keys in ServiceStack

I have such Json:
{
data:{
"50":{"id":"50","name":"test", etc...},
"51":{"id":"51","name":"test", etc...},
"53":{"id":"53","name":"test", etc...},
...
}
}
What is the correct way to deserialize this Json?
[UPDATED]
I think I must to adjust my question. Is it possible to parse Json using class with description of objects. E.g. I have such class and Json which I parse with .FromJson():
public class Data
{
public ...
}
public class Category
{
public int Id{get;set;}
public string Name{get;set;}
}
What should be instead three dots?
Your json contains an object O. This object has a member data that is a dictionary from strings or ints to your category objects. So try something like:
class Root
{
public Dictionary<int, Category> data;
}
var o = JavaScriptSerializer.Deserialize<Root>(json);
If you are using servicestack.text just do
var v = myJson.FromJson();
Don't forget that servicestack is best used when serialization also made with servicestack.
the best way to deserialize Json object to c# class in JSON.NET project (found on codeplex)
the deserialize example is:
JsonConvert.DeserializeObject<Category>(jsonString);

Deserialize Dictionary with JSON.NET

I am using Newtonsoft.Json with version 4.0.8 and trying to use it with Web API.
So I wanted to deserialize JSON with
JsonConvert.DeserializeObject<AClass>(jsonString);
This works until I added a Dictionary as property to this class and wanted to deserialize it.
The json string is in the form of
{
"Date":null,
"AString":"message",
"Attributes":[
{"Key":"key1","Value":"value1"},
{"Key":"key2","Value":"value2"}
],
"Id":0,
"Description":"...
}
When deserializing exception of type JsonSerializationException occures with message: "Cannot deserialize JSON array into type 'System.Collections.Generic.Dictionary`2[System.String,System.String]'."
What am I doing wrong here?
UPDATE1:
When serializing with JSON.NET i get the following for the dictionary:
Attributes":{"key1":"value1","key2":"value2"}
Seems that WebApi deserializes the object in an other way than Json.Net would.
Server side I use following line for implicit deserializing:
return new HttpResponseMessage<AClass>(object);
UPDATE2:
As a workaround I came now to following line server side.
return new HttpResponseMessage<string>(JsonConvert.SerializeObject(license).Base64Encode());
I convert it with Json.Net server side and transfer it as base64 encoded string. So Json.Net can deserialize its own format.
But its still not that what I want, so are thery any further suggestions?
It should work if you declare Attributes as List<KeyValuePair<string, string>>
From this post, calling
JsonConvert.SerializeObject(yourObject, new KeyValuePairConverter());
gets your JSON in the format that the Web API is creating for you.
Ergo, one might assume that calling
JsonConvert.DeserializeObject<AClass>(jsonString, new KeyValuePairConverter());
will do the reverse and correctly handle the Web API's style.
I have no idea whether this overload even exists, though; give it a try and see what happens...
Dictionary<string, object> result = JsonConvert.DeserializeObject<Dictionary<string, object>>(strJsonResult);
If it's .NET 4, you can use DataContract attributes and the DataContractJsonSerializer Class to enforce the message format:
[DataContract]
public class Message
{
[DataMember]
public DateTime? Date { get; set; }
[DataMember]
public string AString { get; set; }
[DataMember]
public Dictionary<string, string> Attributes { get; set; }
[DataMember]
public int Id { get; set; }
[DataMember]
public string Description { get; set; }
}
DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(Message));
Message message = null;
using (MemoryStream jsonStream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)))
{
// Deserialize
message = (Message)jsonSerializer.ReadObject(jsonStream);
// Go to the beginning and discard the current stream contents.
jsonStream.Seek(0, SeekOrigin.Begin);
jsonStream.SetLength(0);
// Serialize
jsonSerializer.WriteObject(jsonStream, message);
jsonString = Encoding.UTF8.GetString(jsonStream.ToArray());
}
Serializing this back out produces the following JSON:
{"AString":"message","Attributes":[{"Key":"key1","Value":"value1"},{"Key":"key2","Value":"value2"}],"Date":null,"Description":"...","Id":0}

Categories