简体   繁体   English

(Rust- JSON) 如何在 rust 中将 &str 转换为 JSON 响应?

[英](Rust- JSON) How to convert &str to JSON response in rust?

Hello I have a &str type variable.您好,我有一个 &str 类型的变量。 How can I convert it to json?如何将其转换为 json? for example I have &str例如我有 &str

{"index":0,"name":"AB/CDE/FG/402/test_int4","sts":"on","time":"2021-06-05 03:28:24.044284300 UTC","value":8}

How can I convert this to json如何将其转换为 json

{
"index": 0,
"name": "AB/CDE/FG/402/test_int4",
"sts": "on",
"time": "2021-06-05 03:28:24.044284300 UTC",
"value": 8 
}

is there anything like python json.loads() method available or something equivalent can be done in rust?是否有类似 python json.loads() 方法可用或可以在 rust 中完成类似的方法?

I dont know anything builtin, but this package can do it:我什么都不知道,但是这个 package 可以做到:

use tinyjson::{JsonParseError, JsonValue};

fn main() -> Result<(), JsonParseError> {
   let s = r#"
{
   "index":0,
   "name":"AB/CDE/FG/402/test_int4",
   "sts":"on",
   "time":"2021-06-05 03:28:24.044284300 UTC",
   "value":8
}
"#;
   let m: JsonValue = s.parse()?;
   println!("{:?}", m);
   Ok(())
}

Result:结果:

Object({
   "value": Number(8.0),
   "index": Number(0.0),
   "name": String("AB/CDE/FG/402/test_int4"),
   "time": String("2021-06-05 03:28:24.044284300 UTC"),
   "sts": String("on")
})

https://docs.rs/tinyjson https://docs.rs/tinyjson

There's a misconception here.这里有一个误解。 JSON is a serialization format, its always a string. JSON 是一种序列化格式,它始终是一个字符串。 Your two blocks are both JSON and functionally identical.你的两个块都是 JSON 并且功能相同。 What json.loads() is deserialize it into a value that can be accessed and manipulated natively. json.loads()将其反序列化为可以本地访问和操作的

You probably want to use a serde_json::Value if you want to this JSON to be used generically.如果您想将此 JSON 用于一般用途,您可能需要使用serde_json::Value

use serde_json::Value;

fn main() {
    let input = r#"{"index":0,"name":"AB/CDE/FG/402/test_int4","sts":"on","time":"2021-06-05 03:28:24.044284300 UTC","value":8}"#;
    let mut object: Value = serde_json::from_str(input).unwrap();
    
    if let Some(name) = object.get_mut("name") {
        *name = "new name".into();
    }
    
    println!("{}", object);
}
{"index":0,"name":"new name","sts":"on","time":"2021-06-05 03:28:24.044284300 UTC","value":8}

Playground 操场

Or, its typical to create a structure that reflects what the JSON is expected to be and deserialize into that type via serde :或者,通常创建一个结构来反映 JSON 预期的内容,并通过serde反序列化为该类型:

use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug)]
struct Data {
    index: i32,
    name: String,
    sts: String,
    time: String,
    value: i32
}

fn main() {
    let input = r#"{"index":0,"name":"AB/CDE/FG/402/test_int4","sts":"on","time":"2021-06-05 03:28:24.044284300 UTC","value":8}"#;
    let mut object: Data = serde_json::from_str(input).unwrap();
    
    object.name = "new name".to_string();
    
    println!("{:#?}", object);
}
Data {
    index: 0,
    name: "new name",
    sts: "on",
    time: "2021-06-05 03:28:24.044284300 UTC",
    value: 8,
}

Playground 操场

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

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