How to show the "paste Json class" in visual studio 2012 when clicking on Paste Special?

Solution 1:

I created a new project, installed Newtonsoft.Json and added a class.

If you have your class file open, copy your Json data and choose Edit -> Paste Special you will see both the options:

Paste Special with Json Option

If you have some other kind of file open (e.g. app.config), you only see "Paste JSON as As Classes" (and it's greyed out)

Paste Special - 1 option and greyed out

It does seem a bit flaky though - sometimes I had to recopy the data before it would show up.

Try a) recopying your data b) playing around with what windows you've got open c) reinstalling the extension

Solution 2:

When selecting the Edit > Paste Special menu while in the code of a class file, make sure that the Visual Studio project that your class file is under has its 'Target Framework' set to:

.NET Framework 3.5+ for 'Paste JSON as Classes'

.NET Framework 4.5+ for 'Paste XML as Classes'

Otherwise these options do not appear.

The 'Target Framework' setting is under the Project Properties > Application.

Solution 3:

You need to install http://www.microsoft.com/en-au/download/details.aspx?id=41532 to get the option to appear under Paste Special.

Solution 4:

I had the same issue. I was selecting the class from the solution explorer, and getting only the XML option. I finally put my carrot directly in the curly braces of the public class, then navigated to Edit->'Paste Special'.

Edit: Correction, that generates a class inside the class. Instead, I'm going to put my insertion point somewhere in the namespace. 'Paste JSON classes' then generates a public class Rootobject{} and other public classes that are nested in the clipboard JSON.

Solution 5:

I had the same problem and discovered that you should have valid JSON text in your clipboard.

Steps to get it to work:

  1. Install Web Essentials for your version of VS (Visual Studio). Web Essentials Download page
  2. Create empty class file in VS.
  3. Copy valid JSON text into clipboard.
  4. You will now see the "Paste Json as Classes" under Edit -> Paste Special -> Paste Json as Classes

Sample input:

    {
       "firstName":"John",
       "lastName":"Smith",
       "age":25,
       "address":{
          "streetAddress":"21 2nd Street",
          "city":"New York",
          "state":"NY",
          "postalCode":"10021"
       },
       "phoneNumber":[
          {
             "type":"home",
             "number":"212 555-1234"
          },
          {
             "type":"fax",
             "number":"646 555-4567"
          }
       ]
    }

Sample output:

    public class Rootobject
    {
        public string firstName { get; set; }
        public string lastName { get; set; }
        public int age { get; set; }
        public Address address { get; set; }
        public Phonenumber[] phoneNumber { get; set; }
    }

    public class Address
    {
        public string streetAddress { get; set; }
        public string city { get; set; }
        public string state { get; set; }
        public string postalCode { get; set; }
    }

    public class Phonenumber
    {
        public string type { get; set; }
        public string number { get; set; }
    }