Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

May I ask how to convert my JSON string into a JSON variable in the template #533

Closed
sunkaifei opened this issue Jul 6, 2024 · 2 comments

Comments

@sunkaifei
Copy link

The fields stored in my database are JSON strings, and I want to convert this field directly into a variable on the template page, and then output each content in JSON through this variable, or is there any other way to directly output it?

@sunkaifei
Copy link
Author

The current solution is

// 自定义过滤器函数
pub fn to_json_filter(value: &Value) -> Result<Value, Error> {
    // 处理输入的 value,假设输入是一个字符串
    let json_str = value.as_str().unwrap_or("");
    match serde_json::from_str::<Value>(json_str) {
        Ok(json_value) => {
            Ok(json_value)
        },
        Err(err) => { 
            log::error!("Error parsing JSON: {}", err);
            Ok(Value::from_safe_string("Json标签解析失败".to_string()))   
        },
    }
}

@mitsuhiko
Copy link
Owner

A custom JSON parsing filter is most likely the right solution. Another option would be to parse the data before it's passed to the template.

Repository owner locked and limited conversation to collaborators Jul 17, 2024
@mitsuhiko mitsuhiko converted this issue into discussion #537 Jul 17, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants