How to Show the "Paste JSON Class" in Visual Studio 2012 When Clicking on Paste Special

Paste C# Class as JSON in VS 2012

There are various approaches you can take. If you are planning to convert the c# classes to json representation, the easiest way is to serialize the data representation using something like a Javascript serializer or JSON.Net. Here is an example that converts a given IEnumerable to its JSON representation

 public static string ToJSONString<T>(this IEnumerable<T> items)
{
var jsonString = JsonConvert.SerializeObject(items, Formatting.Indented, new JsonSerializerSettings
{
ContractResolver = new LowerCaseContractResolver(),
NullValueHandling = NullValueHandling.Ignore
});

jsonString = jsonString.Replace("\r\n", string.Empty);

return jsonString;

}

Another approach (this is for knockout viewmodels, is using T4 Templates to generate it. You can use the same concept to generate your representation if you plan to generate JavaScript files with your model.

How to auto-generate a C# class file from a JSON string

Five options:

  • Use the free jsonutils web tool without installing anything.

  • If you have Web Essentials in Visual Studio, use Edit > Paste special > paste JSON as class.

  • Use the free jsonclassgenerator.exe

  • The web tool app.quicktype.io does not require installing anything.

  • The web tool json2csharp also does not require installing anything.

Pros and Cons:

  • jsonclassgenerator converts to PascalCase but the others do not.

  • app.quicktype.io has some logic to recognize dictionaries and handle JSON properties whose names are invalid c# identifiers.

Deserialize JSON Object into Class

I see two problems here: one is using classes as the dictionary keys - the JSON has simple strings there (and cannot have anything else really), so that won't work.

The second problem is that deserialization of JSON to classes works by matching keys to properties - so it converts something like

{
"prop1": "value1",
"prop2": "value2"
}

to an instance of:

public class MyClass {
public string prop1 { get; set; }
public string prop2 { get; set; }
}

In your case this cannot work because in your JSON all keys are not valid property names. You have to stick with the deserialization to a dictionary

How to convert JSON text into objects using C#

I recommend you to use JSON.NET. it is an open source library to serialize and deserialize your c# objects into json and Json objects into .net objects ...

Serialization Example:

Product product = new Product();
product.Name = "Apple";
product.Expiry = new DateTime(2008, 12, 28);
product.Price = 3.99M;
product.Sizes = new string[] { "Small", "Medium", "Large" };

string json = JsonConvert.SerializeObject(product);
//{
// "Name": "Apple",
// "Expiry": new Date(1230422400000),
// "Price": 3.99,
// "Sizes": [
// "Small",
// "Medium",
// "Large"
// ]
//}

Product deserializedProduct = JsonConvert.DeserializeObject<Product>(json);

Performance Comparison To Other JSON serializiation Techniques
Sample Image

How to parse specific data from JSON response

Copy the raw json to the clipboard. In Visual Studio menu select Edit > Paste Special > Paste JSON As Classes (this item is present at least since version VS2015. For earlier versions see). This will generate a set of classes.

public class Rootobject
{
public User user { get; set; }
public string logging_page_id { get; set; }
}
public class User
{
public string biography { get; set; }
public bool blocked_by_viewer { get; set; }
public bool country_block { get; set; }
public string external_url { get; set; }
public string external_url_linkshimmed { get; set; }
public Followed_By followed_by { get; set; }
public bool followed_by_viewer { get; set; }
public Follows follows { get; set; }
public bool follows_viewer { get; set; }
public string full_name { get; set; }
public bool has_blocked_viewer { get; set; }
public bool has_requested_viewer { get; set; }
public string id { get; set; }
public bool is_private { get; set; }
public bool is_verified { get; set; }
public string profile_pic_url { get; set; }
public string profile_pic_url_hd { get; set; }
public bool requested_by_viewer { get; set; }
public string username { get; set; }
public object connected_fb_page { get; set; }
public Media media { get; set; }
}
public class Followed_By
{
public int count { get; set; }
}
public class Follows
{
public int count { get; set; }
}
public class Media
{
public Node[] nodes { get; set; }
public int count { get; set; }
public Page_Info page_info { get; set; }
}
public class Page_Info
{
public bool has_next_page { get; set; }
public string end_cursor { get; set; }
}
public class Node
{
public string __typename { get; set; }
public string id { get; set; }
public bool comments_disabled { get; set; }
public Dimensions dimensions { get; set; }
public object gating_info { get; set; }
public string media_preview { get; set; }
public Owner owner { get; set; }
public string thumbnail_src { get; set; }
public object[] thumbnail_resources { get; set; }
public bool is_video { get; set; }
public string code { get; set; }
public int date { get; set; }
public string display_src { get; set; }
public string caption { get; set; }
public Comments comments { get; set; }
public Likes likes { get; set; }
public int video_views { get; set; }
}
public class Dimensions
{
public int height { get; set; }
public int width { get; set; }
}
public class Owner
{
public string id { get; set; }
}
public class Comments
{
public int count { get; set; }
}
public class Likes
{
public int count { get; set; }
}

Next, use the Rootobject. It's supposed to work.

var client = new WebClient();
var text = client.DownloadString("https://www.instagram.com/2saleapp/?__a=1");
Rootobject rootObject = JsonConvert.DeserializeObject<Rootobject>(text);
Console.WriteLine("Followers count =" + rootObject.user.followed_by.count);

In general, you should change the naming to conform to the generally accepted naming rules. You should use the JsonProperty attribute.

public class Rootobject
{
[JsonProperty("user")]
public User User { get; set; }
[JsonProperty("logging_page_id")]
public string LoggingPageId { get; set; }
}

And so on.

C# how to deserialize my json file using Newtonsoft?

Your C# class does not match your JSON.

Copy your JSON, go to Visual Studio 2015, open a .cs file, in the menu Edit -> Paste Special -> Paste JSON as classes.

And you get corresponding C# classes:

public class MyItem
{
public Item item { get; set; }
}

public class Item
{
public string icon { get; set; }
public string icon_large { get; set; }
public int id { get; set; }
public string type { get; set; }
public string typeIcon { get; set; }
public string name { get; set; }
public string description { get; set; }
public Current current { get; set; }
public Today today { get; set; }
public string premium { get; set; }
}

public class Current
{
public string trend { get; set; }
public int price { get; set; }
}

public class Today
{
public string trend { get; set; }
public string price { get; set; }
}


Related Topics



Leave a reply



Submit