{ commentId: "52013", userId: "981662180", userName: "222", body: "222", pictureURL: "", commentSource: "222", commentType: 2, contentId: "221111", mId: "123trst" } 转成JSON格式
时间: 2023-07-03 17:31:52 浏览: 105
这段数据已经是JavaScript对象格式了,你可以使用JSON.stringify()将其转换为JSON格式的字符串,示例代码如下:
```javascript
const commentInfoObj = {
commentId: "52013",
userId: "981662180",
userName: "222",
body: "222",
pictureURL: "",
commentSource: "222",
commentType: 2,
contentId: "221111",
mId: "123trst"
};
const commentInfoStr = JSON.stringify(commentInfoObj);
console.log(commentInfoStr);
```
输出结果为:
```
{"commentId":"52013","userId":"981662180","userName":"222","body":"222","pictureURL":"","commentSource":"222","commentType":2,"contentId":"221111","mId":"123trst"}
```
这样就将JavaScript对象转换为了JSON格式的字符串。
相关问题
"commentInfo":"{\"commentId\": \"52013\",\"userId\": \"981662180\",\"userName\": \"222\",\"body\": \"222\",\"pictureURL\": \"\",\"commentSource\": \"222\",\"commentType\": 2,\"contentId\": \"221111\",\"mId\": \"123trst\"}", 前端语言怎么转换
这段字符串看起来是JSON格式的数据,你可以使用JavaScript中的JSON.parse()方法将其转换为JavaScript对象,示例代码如下:
```javascript
const commentInfoStr = "{\"commentId\": \"52013\",\"userId\": \"981662180\",\"userName\": \"222\",\"body\": \"222\",\"pictureURL\": \"\",\"commentSource\": \"222\",\"commentType\": 2,\"contentId\": \"221111\",\"mId\": \"123trst\"}";
const commentInfoObj = JSON.parse(commentInfoStr);
console.log(commentInfoObj);
```
输出结果为:
```javascript
{
commentId: "52013",
userId: "981662180",
userName: "222",
body: "222",
pictureURL: "",
commentSource: "222",
commentType: 2,
contentId: "221111",
mId: "123trst"
}
```
这样就将字符串转换为了JavaScript对象,你可以方便地访问和操作其中的属性值了。
{ "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}
```
阅读全文