python脚本,xxx='{"isSelected":false,"isValidateForm":true,"commentOption":"","commentId":{},"isCommentCreate":true,"hasFiles":false}',xxx.format(1),报错,为什么
时间: 2023-04-04 18:00:21 浏览: 164
答:这是因为 xxx 变量中的字符串中包含了大括号 {},而 format 方法中的数字 1 会被解析为占位符,导致格式化字符串时出错。解决方法是在大括号前面加上一个转义符,即将 {} 改为 \{\},或者使用 f-string 格式化字符串。
相关问题
翻译这段代码 success: (ress) => { DB.collection("collectComment").add({ data: { articleId: this.data.pageId, articleTitle: this.data.articleInfo.title, commentId: res._id, commentContext: this.data.commentContext, flog: false, temp: 1, supportVolume: 0, createTime: app.formatDate(new Date(), 'YYYY-MM-DD HH:mm:ss'), time: +new Date(), userId: app.globalData.userId, userimg: app.globalData.userimg, username: app.globalData.username, },
这是一段 JavaScript 代码,它向数据库中的 "collectComment" 集合中添加一个新的文档。该文档包含了文章的 ID、标题、评论的 ID、评论的内容、标记、临时标志、支持量、创建时间、时间戳、用户 ID、用户头像和用户名等信息。
其中,时间使用了 app.formatDate 函数格式化成了 YYYY-MM-DD HH:mm:ss 的形式,userId、userimg 和 username 采用了全局变量 app.globalData 中的值。成功执行添加操作后,会调用一个回调函数,参数为 ress。
{ "commentId": null, "commentUserId": null, "commentUserName": null, "commentContent": null, "commentPic": null, "commentSource": null, "commentType": null, "mId": null, "pId": null } 转成JSONString
以下是该JSON转成JSON字符串的示例代码(以Java为例):
```java
import com.alibaba.fastjson.JSONObject;
public class Main {
public static void main(String[] args) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("commentId", null);
jsonObject.put("commentUserId", null);
jsonObject.put("commentUserName", null);
jsonObject.put("commentContent", null);
jsonObject.put("commentPic", null);
jsonObject.put("commentSource", null);
jsonObject.put("commentType", null);
jsonObject.put("mId", null);
jsonObject.put("pId", null);
String jsonString = jsonObject.toString();
System.out.println(jsonString);
}
}
```
输出结果为:
```
{"commentId":null,"commentUserId":null,"commentUserName":null,"commentContent":null,"commentPic":null,"commentSource":null,"commentType":null,"mId":null,"pId":null}
```
阅读全文
相关推荐















