使用Paste JSON as Code插件实现json格式转typeScript语法格式

文章介绍了如何在VSCode中使用PasteJSONasCode插件将JSON文件转换为TypeScript接口定义,提供两种操作方法,包括通过命令面板和创建临时文件直接粘贴JSON内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用Paste JSON as Code实现json转typeScript

  1. 【软件】vscode软件

创建test.json文件

{
  "name": "John",
  "age": 30,
  "isStudent": true,
  "address": {
    "street": "123 Main St",
    "city": "New York",
    "country": "USA"
  },
  "hobbies": ["reading", "painting", "playing guitar"],
  "education": {
    "degree": "Bachelor",
    "major": "Computer Science",
    "university": "ABC University",
    "completed": true
  },
  "friends": [
    {
      "name": "Sarah",
      "age": 28,
      "address": {
        "street": "456 Elm St",
        "city": "Los Angeles",
        "country": "USA"
      },
      "isCloseFriend": true
    },
    {
      "name": "Michael",
      "age": 32,
      "address": {
        "street": "789 Oak St",
        "city": "Chicago",
        "country": "USA"
      },
      "isCloseFriend": false
    }
  ],
  "hasPets": false
}

ctrl+shift+P打开命令面板或查看-命令面板

输入json,选择Open quicktype for JSON,即可生成QuickType.ts文件

// Generated by https://quicktype.io
//
// To change quicktype's target language, run command:
//
//   "Set quicktype target language"

export interface Test {
    name:      string;
    age:       number;
    isStudent: boolean;
    address:   Address;
    hobbies:   string[];
    education: Education;
    friends:   Friend[];
    hasPets:   boolean;
}

export interface Address {
    street:  string;
    city:    string;
    country: string;
}

export interface Education {
    degree:     string;
    major:      string;
    university: string;
    completed:  boolean;
}

export interface Friend {
    name:          string;
    age:           number;
    address:       Address;
    isCloseFriend: boolean;
}

复制到相对应的ts文件中编修即可,非常方便。


另外一种操作方式

ctrl+N创建一个临时文件 Untitled-1,随意输入以下代码,并ctrl+A全选复制,创建test.ts文件,ctrl+shift+p输入json,选择Paste JSON as Code。根据提示输入名称(Paste)回车。

{
    "name": "John",
    "age": 30,
    "isStudent": true,
    "address": {
      "street": "123 Main St",
      "city": "New York",
      "country": "USA"
    },
    "hobbies": ["reading", "painting", "playing guitar"],
    "education": {
      "degree": "Bachelor",
      "major": "Computer Science",
      "university": "ABC University",
      "completed": true
    },
    "friends": [
      {
        "name": "Sarah",
        "age": 28,
        "address": {
          "street": "456 Elm St",
          "city": "Los Angeles",
          "country": "USA"
        },
        "isCloseFriend": true
      },
      {
        "name": "Michael",
        "age": 32,
        "address": {
          "street": "789 Oak St",
          "city": "Chicago",
          "country": "USA"
        },
        "isCloseFriend": false
      }
    ],
    "hasPets": false
  }
  
// Generated by https://quicktype.io

export interface Paste {
  name:      string;
  age:       number;
  isStudent: boolean;
  address:   Address;
  hobbies:   string[];
  education: Education;
  friends:   Friend[];
  hasPets:   boolean;
}

export interface Address {
  street:  string;
  city:    string;
  country: string;
}

export interface Education {
  degree:     string;
  major:      string;
  university: string;
  completed:  boolean;
}

export interface Friend {
  name:          string;
  age:           number;
  address:       Address;
  isCloseFriend: boolean;
}

市场:Paste JSON as Code - Visual Studio Marketplace

### Vue3 中配置富文本编辑器 #### 一、安装依赖库 为了在 Vue3 项目中集成富文本编辑器,通常会使用 `tinymce` 或者 `wangEditor` 这样的第三方组件。对于 wangEditor 的情况,可以通过 npm 安装最新版本: ```bash npm install wangeditor --save ``` 而对于 tinymce,则需执行如下命令来获取其资源以及 vue 版本适配包[^2]: ```bash npm i @tinymce/tinymce-vue tinymce -S ``` #### 二、创建富文本编辑器组件 接下来,在 src/components 文件夹下新建 RichTextEditor.vue 组件用于封装所选的富文本编辑器。 如果采用 WangEditor 实现,那么可以在 `<script setup>` 标签内初始化 editor 并设置相关属性[^1]: ```javascript import E from 'wangeditor' import { onMounted, ref } from 'vue' export default { name: "RichTextEditor", props: ['modelValue'], emits: ['update:modelValue'], setup(props, context){ const editor = new E('#editor') let value = ref('') // 将输入框的内容同步给父级组件 function changeHtml(value) { context.emit('update:modelValue', value); } onMounted(() => { editor.config.onchange = (newHtml) => { changeHtml(newHtml) } // 支持粘贴 Word 文档中的内容并处理其中的图片 editor.config.pasteFilterStyle = false; editor.config.uploadImgServer = '/your/upload/api';// 图片上传接口地址 editor.create(); value.value = props.modelValue || ''; }) return{ value, editor }; }, } ``` 当选择 Tinymce 方案时,按照官方文档指引引入 TinyMCE 编辑器实例,并通过 options 参数自定义功能特性: ```html <template> <editor v-model="content" :init="editorInitOptions"/> </template> <script lang="ts"> import Editor from '@tinymce/tinymce-vue'; import axios from 'axios'; const API_URL = process.env.VUE_APP_API_BASEURL; export default defineComponent({ components: { Editor }, data() { return { content: '', editorInitOptions:{ height: 500, menubar: true, plugins: [ 'advlist autolink lists link image charmap print preview anchor', 'searchreplace visualblocks code fullscreen', 'insertdatetime media table paste code help wordcount' ], toolbar: 'undo redo | formatselect | bold italic backcolor | \ alignleft aligncenter alignright alignjustify | \ bullist numlist outdent indent | removeformat | help', images_upload_handler(blobInfo, success, failure) { var xhr, formData; xhr = new XMLHttpRequest(); xhr.withCredentials = false; xhr.open('POST', `${API_URL}/upload`); xhr.onload = () => { let json; if (xhr.status != 200) { failure('HTTP Error: ' + xhr.status); return; } json = JSON.parse(xhr.responseText); if (!json || typeof json.location != 'string') { failure('Invalid JSON: ' + xhr.responseText); return; } success(json.location); }; formData = new FormData(); formData.append('file', blobInfo.blob(), blobInfo.filename()); xhr.send(formData); } } } }); </script> ``` #### 三、注册全局组件以便于在整个应用范围内调用 最后一步是在 main.js/main.ts 中声明上述创建好的富文本编辑器作为全局可用组件: ```typescript import App from './App.vue' import RichTextEditor from '@/components/RichTextEditor.vue' app.component('richTextEditor', RichTextEditor) createApp(App).mount('#app') ``` 现在就可以在任何页面里轻松地利用 `<richTextEditor />` 来实现强大的富文本编辑能力了!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Vinca@

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值