0%

用到的serde_json

1 几个对象

  • 字符串: r#"{ "name": "John Doe"}"#
  • serde_json::Value: json内部结构
  • struct Person{name: String}: rust内部结构

2 转换

2.1 字符串 –> Value

1
2
3
4
let data = r#"{ "name": "John Doe"}"#;
let v: Value = serde_json::from_str(data)?;

let v = json!({"name":"hi"});

2.2 字符串 –> struct

1
2
let data = r#"{ "name": "John Doe"}"#;
let v: Person = serde_json::from_str(data)?;

2.3 Value –> 字符串

1
2
let v = json!({"name":"hi"});
let s = v.to_string();

2.4 Value –> struct

1
let p:Persion = serde_json::from_value(jv)?;

2.5 struct –> 字符串

1
let j:String = serde_json::to_string(&person)?;

2.6 struct –> Value

1
let jv: Value = serde_json::to_value(person)?;

3 修改

1
2
jv["a"] = json!("world");
jv["b"] = json!({"hello": "world"});