5

quicktype在线工具:将JSON自动转为各种编程语言

 1 year ago
source link: https://www.jdon.com/62912
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

quicktype在线工具:将JSON自动转为各种编程语言


将JSON转换为Swift、C#、TypeScript、Objective-C、Go、Java、C++等源代码。
例如下面是JSON:

{
  "greeting": "Welcome to quicktype!",
  "instructions": [
    "Type or paste JSON here",
    "Or choose a sample above",
    "quicktype will generate code in your",
    "chosen language to parse the sample data"
  ]
}

通过这个在线工具自动转换为Rust代码:

extern crate serde_derive;

#[derive(Serialize, Deserialize)]
pub struct Welcome {
    greeting: String,
    instructions: Vec<String>,
}

Go代码:

package main

import "encoding/json"

func UnmarshalWelcome(data byte) (Welcome, error) {
    var r Welcome
    err := json.Unmarshal(data, &r)
    return r, err
}

func (r *Welcome) Marshal() (byte, error) {
    return json.Marshal(r)
}

type Welcome struct {
    Greeting     string   `json:"greeting"`    
    Instructions string `json:"instructions"`
}

点击标题进入工具


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK