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 have a JSON string as follows
string str = "{"Id":["1799"],"Type":1,"Date":null,"Group":null,"Ids":1799}";
I want to covert it to the following format
{"Id":1799,"Type":1,"Date":null,"Group":null }
In short I want to remove the "Ids" and convert "Id" value to string.
For this I tried deserializing this string as follows-
object yourOjbect = new JavaScriptSerializer().DeserializeObject(str);
But here I am stuck. How Can I remove/change a value from this object.
I tried converting this object to array and list but could not find remove/modify option in it.
Maybe you can use the JSON framework for .NET from http://www.newtonsoft.com/json (also available as a nuget package)
Then you can use the following to Deserialize into your object
string str = "{"Id":["1799"],"Type":1,"Date":null,"Group":null,"Ids":1799}";
MyObject myObj = JsonConvert.DeserializeObject<MyObject>(json);
Then maybe create a different object for your output, and have a constructor that will accept the original object as input, and then serialise it to Json. The constructor must then do any internal conversions/changes that you require.
OtherObject other = new OtherObject(myObj); //Create new object from original.
string json = JsonConvert.SerializeObject(other);
Expando Object.
string str = "{"Id":["1799"],"Type":1,"Date":null,"Group":null,"Ids":1799}";
Initially, deserialize the json using NewtonJson lib
dynamic parsedJson = JsonConvert.DeserializeObject<dynamic>(str);
Dynamic newStr = new ExpandoObject();
newStr.Id = parsedJson.Id.ToString();
newStr.Type = parsedJson.Type;
...
then serilize the newStr:
str newJson = JsonConvert.SerializeObject(newStr);
Output :
{"Id":1799,"Type":1,"Date":null,"Group":null }
I got a json Object that i want to deserialize to its .Net type without casting it.
I think i read somewhere in the doc that you can pass an attribute into the json to tells to the deserializer the .Net object type that it can try to cast.
I can't find the where i read this.
I want to avoid use of
var myNewObject = JsonConvert.DeserializeObject<MyClass>(json);
To get something like this
MyClass myNewObject = JsonConvert.DeserializeObject(json);
I got my json object from an HttpRequest and want to instantiate the appropriate class from this nested object.
Currently deserialization into an known item work good but need something more flexible without the need to manage all known Object from a parsing method.
You can save the object type in your json string like this.
The settings you have to hand over the converter
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Objects
};
How to serialize with the given settings:
var json = JsonConvert.SerializeObject(data, Settings);
This is what your json string looks like:
{
"$type":"YourNamespaceOfTheClass",
"YourPropertyInTheClass":valueOfProperty
}
How to deserialize with the given settings:
var object = JsonConvert.DeserializeObject(json, Settings);
Now your json string contains not only the serialized object, but also the type of your serialized object. So you don't have to worry about the right type when deserializing your json string.
You can do the following:
dynamic myNewObject = JsonConvert.DeserializeObject(json);
which will return a dynamic object which you can work with.
Console.WriteLine(myNewObject.data[0].description);
Obviously, it will fail if your JSON doesn't contain data array having objects with description property.
You can do something like this:
var result = JsonConvert.DeserializeObject<dynamic>(json);
so you can deserialize any object. Cast dynamic says that you're deserializing an anynymous object with any type which will be known on runtime. It trully works!
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.
I have a JSON string in this form:
string jsonStr = "[\"A\", [\"Martini\", \"alovell\"],[\"Martin\", \"lovell\"]]"
I am trying to deserialize the JSON using the C# .NET deserializer DataContractJsonSerializer with the following code snippet
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonStr));
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof<X>);
X data = (X)serializer.ReadObject(ms);
Now since the JSON array is an array of variable types I do not know what type of object
X should be
If my String were
jsonStr = "[[\"Martini\", \"alovell\"],[\"Martin\", \"lovell\"]]"
I could use this:
X = List<List<String>>
and that would work for me. I was wondering if there is any way to deserialize variable type JSON array?
You could use Json.NET to do this.
JArray a = JArray.Parse(jsonStr);
The JArray would contain either strings or nested JArray's depending on the JSON.