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?

5
  • Wrong Tag if you want it to do in c#. You already tried something? Commented Nov 24, 2017 at 7:04
  • I havent tried anything..dont have any idea Commented Nov 24, 2017 at 7:06
  • Try this: stackoverflow.com/questions/8291680/… you may get some idea Commented Nov 24, 2017 at 7:07
  • 4
    It is already json. What do you want exactly ? Commented Nov 24, 2017 at 7:08
  • take a look at Json.NET Commented Nov 24, 2017 at 7:08

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

u an also use,

JObject.Parse(urJsonString);

JObject is in Newtonsoft.Json.Linq namespace.

Comments

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());

4 Comments

i wrote string like you tell but after that it is showing error like ] expected, ; expected etc.
it is still giving error like ; missing , unrecognized escape sequence etc.
nd btw, what is obj1?
Here You need to define obj1 ,Its should be a class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.