1

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?

learner
  • 47
  • 1
  • 1
  • 6

3 Answers3

5

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.

JBoothUA
  • 3,040
  • 3
  • 17
  • 46
2

u an also use,

JObject.Parse(urJsonString);

JObject is in Newtonsoft.Json.Linq namespace.

Sarvagya Saxena
  • 229
  • 1
  • 9
1

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());
UJS
  • 853
  • 1
  • 10
  • 16