简体   繁体   English

如何在Visual Studio 2012中单击“选择性粘贴”时显示“粘贴Json类”?

[英]How to show the “paste Json class” in visual studio 2012 when clicking on Paste Special?

I am trying to use the past special feature in vs 2012 in order to generate c# class for my Json data. 我正在尝试使用vs 2012中的过去特殊功能,以便为我的Json数据生成c#类。 I downloaded the Json.New from NewtonSoft from Nuget and then added a new .cs class the copied my json data to clipboard but when I go to Edit-> Paste Special --> I can only see: Paste XML As Classes only the Paste Json as Classes feature is not showing. 我从Nuget从NewtonSoft下载了Json.New,然后添加了一个新的.cs类,将我的json数据复制到剪贴板,但是当我转到Edit-> Paste Special - >时,我只能看到:将XML粘贴为类只有粘贴Json as Classes功能未显示。 Any Suggestions? 有什么建议? Thanks you in advance 提前谢谢你

This is what I am getting and please note that I have installed Newtonsoft.Json already: 这是我得到的,请注意我已经安装了Newtonsoft.Json:


(source: indevcogroup.com ) (来源: indevcogroup.com

I created a new project, installed Newtonsoft.Json and added a class. 我创建了一个新项目,安装了Newtonsoft.Json并添加了一个类。

If you have your class file open, copy your Json data and choose Edit -> Paste Special you will see both the options: 如果您打开了类文件,请复制Json数据并选择编辑 - >选择性粘贴,您将看到两个选项:

使用Json选项选择特殊粘贴

If you have some other kind of file open (eg app.config), you only see "Paste JSON as As Classes" (and it's greyed out) 如果您打开了其他类型的文件(例如app.config),您只会看到“将JSON粘贴为类”(并且它显示为灰色)

选择性粘贴 -  1选项并显示为灰色

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 尝试a)重新复制你的数据b)玩弄你打开的窗户c)重新安装扩展程序

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: 在类文件的代码中选择“ 编辑”>“选择性粘贴”菜单时,请确保您的类文件所在的Visual Studio项目的“目标框架”设置为:

.NET Framework 3.5 + for 'Paste JSON as Classes' .NET Framework 3.5 +用于“将JSON粘贴为类”

.NET Framework 4.5 + for 'Paste XML as Classes' .NET Framework 4.5 +用于“将XML粘贴为类”

Otherwise these options do not appear. 否则这些选项不会出现。

The 'Target Framework' setting is under the Project Properties > Application . “目标框架”设置位于“ 项目属性”>“应用程序”下

您需要安装http://www.microsoft.com/en-au/download/details.aspx?id=41532才能在“选择性粘贴”下显示该选项。

I had the same issue. 我遇到过同样的问题。 I was selecting the class from the solution explorer, and getting only the XML option. 我从解决方案资源管理器中选择了类,并且只获取了XML选项。 I finally put my carrot directly in the curly braces of the public class , then navigated to Edit->'Paste Special'. 我终于将胡萝卜直接放在public class的花括号中,然后导航到编辑 - >'选择特殊粘贴'。

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. '粘贴JSON类'然后生成公共类Rootobject {}和嵌套在剪贴板JSON中的其他公共类。

I had the same problem and discovered that you should have valid JSON text in your clipboard. 我有同样的问题,发现你应该在剪贴板中有有效的JSON文本。

Steps to get it to work: 让它工作的步骤:

  1. Install Web Essentials for your version of VS (Visual Studio). 为您的VS(Visual Studio)版本安装Web Essentials。 Web Essentials Download page Web Essentials下载页面
  2. Create empty class file in VS. 在VS中创建空类文件。
  3. Copy valid JSON text into clipboard. 将有效的JSON文本复制到剪贴板。
  4. You will now see the "Paste Json as Classes" under Edit -> Paste Special -> Paste Json as Classes 现在,您将在Edit - > Paste Special - >将Json粘贴为Classes下看到“将Json粘贴为类”

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; }
    }

尝试为Visual Studio安装更新我安装了更新v4,现在我有一个选项“将JSON粘贴为类”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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