简体   繁体   中英

Generate C# class from JSON stream and Parse this stream

I want to generate C# class from this ( JSON DATA ) but http://json2csharp.com/ can't generate the C# class. The JSON should be valid ( http://jsonlint.com/ ). Can you help me?

And when I create the class form JSON I only use something like this:

MyNewClass test = ser.Deserialize<MyNewClass>(response);

You don't need any class since your json is List<List<string>>

var result = new JavaScriptSerializer().Deserialize<List<List<string>>>(json);

or using Json.Net

var result = JsonConvert.DeserializeObject<List<List<string>>>(json);

That is all....

foreach (var list in result)
{
    foreach (var item in list)
        Console.Write(item + " ");
    Console.WriteLine();
}

You need to create your C# class. So, for example;

public class Wrapper
{   
    public List<CustomObject> Data { get; set; }
}

public class CustomObject 
{ 
    public string Id {get;set;}
    public string Name {get;set;}
}

and then deserialize with System.Web.Script.Serialization.JavaScriptSerializer()

Wrapper wrapper = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<Wrapper>(json);

The easyest and simplest way in 2 steps without any 3rth party libraries

1- go to http://json2csharp.com/ and let the generator to create your c# classes

2-

HttpResponseMessage response = await client.GetAsync(Url);    
YourJSonClass obj = await response.Content.ReadAsAsync<YourJSonClass>();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM