Je suis débutant en C #, j'ai une très longue chaîne et je veux la convertir en json, la chaîne est

{"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"}]}]},
\

Des idées?

c#
1
learner 24 nov. 2017 à 10:03

3 réponses

Meilleure réponse

Vous pouvez faire quelque chose comme ça:

var jobject = JsonConvert.DeserializeObject<JObject>(yourVariable);

Cela utilise la bibliothèque json de Newtonsoft que vous pouvez obtenir de nuget.

JObject est également l'équivalent C # d'un objet JSON, c'est donc probablement quelque chose que vous voudrez utiliser.

4
JBoothUA 24 nov. 2017 à 07:09

Vous devez désérialiser votre chaîne comme suit:

// 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());
0
UJS 24 nov. 2017 à 07:27

U une utilisation également,

JObject.Parse(urJsonString);

JObject est dans l'espace de noms Newtonsoft.Json.Linq.

2
Sarvagya Saxena 10 janv. 2019 à 06:12
47467920