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"`
}

点击标题进入工具