I have a DynamicJsonObject like:
var obj = new DynamicJsonObject();
obj.Field1 = "field1";
obj.Field2 = "field2";
I need the obj's json string. I tried using JavaScriptSerializer:
var json = JavaScriptSerializer.Serialize(obj);
But the result is always json == '{}'
Is there a workaround for this? preferably not using third party libraries
You can add custom converter to JavaScriptSerializer. In System.Web.Helpers one already exists but is internal - you can use the following code to register it:
var type = Type.GetType("System.Web.Helpers.DynamicJavaScriptConverter, System.Web.Helpers");
var converter = (JavaScriptConverter)Activator.CreateInstance(type);
JavaScriptSerializer serializer = new JavaScriptSerializer();
serializer.RegisterConverters(new[] { converter });
var json = serializer.Serialize(obj);
or copy code from here
Thanks for your answers, but I've found a simple way to do this by using System.Web.Helpers.Json.
So, my code looks like this:
string json = Json.Encode(obj);
I cannot use an anonymous object, because I don't create obj, it is provided in the DynamicJsonObject 'format' already.
Related
Normally I'm using Newtonsoft to deserialize like this
List<myObject> deserializeObj = JsonConvert.DeserializeObject<List<myObject>>(mysample);
But now, i'm facing a problem where the attribute of mysample can be dynamic which is user define themselves. Thus i cannot use myObject anymore as it is fixed class. So how can i deserialize object like that?
For example the mysample can be something like bellow and etc:
[{"Name":"a","Phone":"a","Ic":"a"},{"Name":"b","Phone":"b","Ic":"b"}]
OR
[{"Id":"a"},{"Id":"b"}]
Target Framework is .NET Framework 3.5
you can use Dynamic Type
List<dynamic> deserializeObj = JsonConvert.DeserializeObject<List<dynamic>>(mysample);
.NET Framework 3.5 :
List<object> deserializeObj = JsonConvert.DeserializeObject<List<object>>(mysample);
well you can then use reflection to access value
sample:
System.Reflection.PropertyInfo pi = item.GetType().GetProperty("name");
String name = (String)(pi.GetValue(item, null));
dotnt forget to add using System.Reflection;
To avoid using dynamic, you can parse the json using JArray
JArray array = JArray.Parse(json);
Thus i cannot use myObject anymore as it is fixed class. So how can i deserialize object like that?
To get rid from this problem Newtonsoft.Json have very well feature that we can use
If you don't know which json object or array comes from your resource. means you can't determine its c# respective object then newtonsoft have JObject and JArray can handle this problem like
1) Suppose your json string is object like
var json = #"{ 'Name':'a','Phone':'a','Ic':'a'}";
Then you can use JObject here
JObject jObject = JsonConvert.DeserializeObject<JObject>(json);
2) Suppose your json string is array like
var json1 = #"[{ 'Name':'a','Phone':'a','Ic':'a'},{ 'Name':'b','Phone':'b','Ic':'b'}]";
Then you can use JArray here
JArray jArray = JsonConvert.DeserializeObject<JArray>(json1);
After successfully getting JArray from your json string. you can also querying on your deserialized object to get particular object from it like
JObject jObject1 = jArray.Children<JObject>().FirstOrDefault();
JObject jObject2 = jArray.Children<JObject>().FirstOrDefault(x => x["Name"] != null && x["Name"].ToString() == "a");
int count = jArray.Children<JObject>().Count();
If you want to get certain key:value pair from your json then you can get it by below code
JProperty jProperty = jObject1.Properties().Where(x => x.Name == "Name").FirstOrDefault();
var value = (string)jProperty.Value;
Try once may it help you.
i am beginner to C#, i have a very long string and want to convert it to json, string is
{"employees":
[{"id":0,"level":0,"label":"Product Names","subitems":
[{"id":0,"level":1,"label":"Soren","subitemslevel3":
[{"id":0,"level":2,"label":"AAAA"},{"id":0,"level":2,"label":"bbb"}]},
{"id":0,"level":1,"label":"Test","subitemslevel3":
[{"id":0,"level":2,"label":"111"},{"id":0,"level":2,"label":"2222"}]}]},
\
any ideas?
You can do something like this:
var jobject = JsonConvert.DeserializeObject<JObject>(yourVariable);
this is using Newtonsoft's json library that you can get from nuget.
Also JObject is the C# equivalent to a JSON object so that's probably something you'll want to use.
u an also use,
JObject.Parse(urJsonString);
JObject is in Newtonsoft.Json.Linq namespace.
You need to Deserialize your string as follows :
// Your string here
string str = #"[{"categories":
[{"id":0,"level":0,"label":"Product Names","subitems":
[{"id":0,"level":1,"label":"Soren","subitemslevel3":
[{"id":0,"level":2,"label":"AAAA"},{"id":0,"level":2,"label":"bbb"}]},
{"id":0,"level":1,"label":"Test","subitemslevel3":
[{"id":0,"level":2,"label":"111"},{"id":0,"level":2,"label":"2222"}]}]},
{"id":0,"level":0,"label":"Product texts","subitems":
[{"id":0,"level":1,"label":""},{"id":0,"level":1,"label":"<p>Disney <strong>Princess<\/strong><\/p>\n"},{"id":0,"level":1,"label":"<p>Machines & Mechanisms - <strong>Middle<\/strong><\/p>\n"},{"id":0,"level":1,"label":"64738574"},{"id":0,"level":1,"label":"8765432"},{"id":0,"level":1,"label":"a"},{"id":0,"level":1,"label":"aa"},{"id":0,"level":1,"label":"ab"},{"id":0,"level":1,"label":"abe"},{"id":0,"level":1,"label":"aD!?"},{"id":0,"level":1,"label":"Bionicle"},{"id":0,"level":1,"label":"Disney Princess"},{"id":0,"level":1,"label":"er"},{"id":0,"level":1,"label":"foo foo"},{"id":0,"level":1,"label":"hej"},{"id":0,"level":1,"label":"igen"},{"id":0,"level":1,"label":"meta"},{"id":0,"level":1,"label":"metadata"},{"id":0,"level":1,"label":"metadata from kafka"},{"id":0,"level":1,"label":"Metatest"},{"id":0,"level":1,"label":"q"},{"id":0,"level":1,"label":"Simpsons"},{"id":0,"level":1,"label":"test"},{"id":0,"level":1,"label":"Test 123"},{"id":0,"level":1,"label":"Test Metadata 123"}]},{"id":0,"level":0,"label" :"Specialist Terms","subitems":[{"id":0,"level":1,"label":"meta"},{"id":0,"level":1,"label":"new category 1111"},{"id":0,"level":1,"label":"secret category"}]}]}]";
// DeSerialize your object
JavaScriptSerializer serializer1 = new JavaScriptSerializer();
object obje = serializer1.Deserialize(str, obj1.GetType());
I need to pass a Json object to an API, but the API requires the Json properties to have a double label of sorts, such as:
{
"name:id":"1234"
}
However, using Newtonsoft.Json.Linq, I can't get this to format the label exactly. Here is what I've tried so far (which throws an error)
dynamic json= new JObject();
json.name.id = "1234";
Doing
json.id = "1234";
Works just fine. I have also tried
json.name = new JProperty("id", "1234");
Which also throws an error. I have also tried hard coding the json file as a single string and converting that to a JObject, which also threw an error. Is what I'm trying to do possible or am I missing something? Is there another Json package I could use that would support what I want to do?
Use JObject's string indexer notation.
dynamic json = new JObject();
json["name.id"] = "1234";
Since the json is essentially built as a key/value pair, using a string indexer can allow you to overcome atypical property names.
There multiple ways to achieve that.
You can use JsonProperty attribute and specify the property name as name:id like:
class MyClass
{
[JsonProperty("name:id")]
public string Name_Id { get; set; }
}
and then you can do:
MyClass obj = new MyClass();
obj.Name_Id = "1234";
var strJson = JsonConvert.SerializeObject(obj);
and you will get back:
{"name:id":"1234"}
I have a struct, I want to convert it to JSON and save it as local file.
I couldn't find any source that explain how to convert a C# struct into a JSON.
I am using a console application for that, not a webservice/web, etc.
JavaScriptSerializer Class
var serializer = new JavaScriptSerializer();
YourStruct myStruct = new YourStruct(x,y,z);
var json = serializer.Serialize(myStruct);
JSON.NET
The other alternative JSON.net, it do not depends on System.Web.* assemblies:
YourStruct myStruct = new YourStruct(x,y,z);
var json = JsonConvert.SerializeObject(myStruct);
I would recommend using JSon.net. You can then do something like:
string json = JsonConvert.SerializeObject(myObj); // myObj is the struct you want to serialize
File.WriteAllText("Foo.json", json); //Write the text to Foo.json
Install https://www.nuget.org/packages/Newtonsoft.Json/
Use JsonConvert.SerializeObject(structName) to serialize object. Do not forget to change argument name to yours
In C#, I have successfully serialized an anonymous object into JSON by use of code like this...
var obj = new { Amount = 108, Message = "Hello" };
JavaScriptSerializer serializer = new JavaScriptSerializer();
String output = serializer.Serialize(obj);
However, what I would like to be able to do later is to deserialize the JSON string back into an anonymous object. Something like this...
var obj2 = serializer.Deserialize(output, object);
But the serializer.Deserialize() method requires a second parameter that is the type of object it will deserialize to.
I tried this...
var obj2 = serializer.Deserialize(output, obj.GetType());
But this produces an error:
No parameterless constructor defined for type of '<>f__AnonymousType0`2[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'.
I'm not sure what this error means.
how about dynamics, the fastest way I see is this:
dynamic myObject = JsonConvert.DeserializeObject<dynamic>(output);
decimal Amount = Convert.ToDecimal(myObject.Amount);
string Message = myObject.Message;
Note:
You will need Newtonsoft.json.dll reference
JSON.Net is a powerful library to work with JSON in .Net
There's a method DeserializeAnonymousType you can tap in to.
Update: Json.Net is now included with ASP.Net, however my latest favorite that I use is JsonFX. It's got great linq support as well, check it out.
Update 2: I've moved on from JsonFX, and currently use ServiceStack.Text, it's fast!
How about using the DeserializeObject method, it does not require a specific type. This also solved a similar SO question. The method deserializes to a Dictionary<string, object> containing name/value pairs.
Update: to clarify the error you get when doing this:
var obj2 = serializer.Deserialize(output, obj.GetType());
Given the type of obj, Deserialize will try to create a new instance of the type using a default constructor. Anonymous types in C# does not have a public parameterless constructor, and thus the operation fails.
This can also be done using the in-built JavaScriptSerializer, as follows:
object result = new JavaScriptSerializer().DeserializeObject(JSONData);
This will return an object[] instance, with Key-Value pairs.
Recently I have been using the awesome JsonFx.Net library and I've come to appreciate what it does. You can use Nuget Package Manager to install it right inside Visual Studio.
The code goes like this,
var reader = new JsonReader();
string input = #"{ ""first"": ""Foo"", ""last"": ""Bar"" }";
var template = new { first=String.Empty, middle=String.Empty, last=String.Empty };
var output = reader.Read(input, template);
As you can see you can even specify the template for Anonymous Type.
if you use Newtonsoft.Json you can try DeserializeAnonymousType method
var obj1 = new { Amount = 108, Message = "Hello" };
var json=JsonConvert.SerializeObject(obj1);
// or as well
var json= "{ \"Amount\" : 108, \"Message\" : \"Hello\" }";
//Deserialization
var definition = new { Amount = 0, Message = "" };
//obj2 type is "anonymous"
var obj2 = JsonConvert.DeserializeAnonymousType(json,definition);
result
{ Amount = 108, Message = "Hello" }
If you do not want to manually provide the type i found the easyest way is just:
var jsonString = JsonHelper.SerializeObject(item);
ExpandoObject obj = JsonHelper.DeserializeObject<ExpandoObject>(jsonString);
You can use JObject instead to deserialize the JSON string:
using Newtonsoft.Json.Linq;
string output = "{\"Amount\" = 108, \"Message\" = \"Hello\"}";
var amount = JObject.Parse(output)["Amount"];