使用Jquery从Json返回数据中删除双引号
remove double quotes from Json return data using Jquery
我使用JQuery来获取Json数据,但是它显示的数据有双引号。有一个函数可以去除它吗?
$('div#ListingData').text(JSON.stringify(data.data.items[0].links[1].caption))
它返回:
"House"
如何删除双引号?欢呼。
使用replace
:
var test = "'"House'"";
console.log(test);
console.log(test.replace(/'"/g, ""));
// "House"
// House
注意末尾的g
表示"global"(替换所有)。
当你像你的例子一样了解你的数据时,对于利基需求…
JSON.parse(this_is_double_quoted);
JSON.parse("House"); // for example
stringfy
方法不是用于解析JSON,而是用于将对象转换为JSON字符串。
JSON在加载时由jQuery解析,您不需要解析数据就可以使用它。只使用数据中的字符串:
$('div#ListingData').text(data.data.items[0].links[1].caption);
这里有人建议使用eval()
从字符串中删除引号。不要那样做,那只是乞求代码注入。
let message = JSON.stringify(your_json_here); // "Hello World"
console.log(JSON.parse(message)) // Hello World
我也有这个问题,但在我的情况下,我不想使用正则表达式,因为我的JSON值可能包含引号。希望我的回答能在将来对别人有所帮助。
我通过使用标准字符串切片来删除第一个和最后一个字符来解决这个问题。这对我来说是有效的,因为我在产生它的textarea
上使用了JSON.stringify()
,因此,我知道我总是会在字符串的每个末端使用"
s。
在这个通用示例中,response
是AJAX返回的JSON对象,key
是JSON键的名称。
response.key.slice(1, response.key.length-1)
我这样用一个正则表达式replace
来保存换行符,并将该键的内容写入HTML中的段落块:
$('#description').html(studyData.description.slice(1, studyData.description.length-1).replace(/''n/g, '<br/>'));
在本例中,$('#description')
是我要写入的段落标签。studyData
是我的JSON对象,description
是我的多行值键
您可以简单地尝试String();删除引号
参考这里的第一个例子:https://www.w3schools.com/jsref/jsref_string.asp
谢谢我。
PS:给mod:不要误会我在挖掘死去的老问题。我今天遇到了这个问题,我在寻找答案的时候看到了这篇文章,我只是把答案贴出来。
您正在做的是在您的示例中创建一个JSON字符串。要么不要使用JSON.stringify()
,或者如果你有JSON数据回来,你不想要引号,只需使用JSON.parse()
来删除JSON响应周围的引号!不要使用正则表达式,没有必要。
我不认为有必要替换任何引号,这是一个完美的JSON字符串,你只需要将JSON字符串转换为对象。这篇文章很好地解释了这种情况:Link
的例子:
success: function (data) {
// assuming that everything is correct and there is no exception being thrown
// output string {"d":"{"username":"hi","email":"hi@gmail.com","password":"123"}"}
// now we need to remove the double quotes (as it will create problem and
// if double quotes aren't removed then this JSON string is useless)
// The output string : {"d":"{"username":"hi","email":"hi@gmail.com","password":"123"}"}
// The required string : {"d":{username:"hi",email:"hi@gmail.com",password:"123"}"}
// For security reasons the d is added (indicating the return "data")
// so actually we need to convert data.d into series of objects
// Inbuilt function "JSON.Parse" will return streams of objects
// JSON String : "{"username":"hi","email":"hi@gmail.com","password":"123"}"
console.log(data); // output : Object {d="{"username":"hi","email":"hi@gmail.com","password":"123"}"}
console.log(data.d); // output : {"username":"hi","email":"hi@gmail.com","password":"123"} (accessing what's stored in "d")
console.log(data.d[0]); // output : { (just accessing the first element of array of "strings")
var content = JSON.parse(data.d); // output : Object {username:"hi",email:"hi@gmail.com",password:"123"}" (correct)
console.log(content.username); // output : hi
var _name = content.username;
alert(_name); // hi
}
我有类似的情况在一个承诺刚刚解决做cast作为(String)
export async function getUserIdByRole(userRole: string): Promise<string> {
const getRole = userData.users.find((element) => element.role === userRole);
return String (getRole?.id);
}
- Laravel 5.2:如何使用Ajax发布和删除数据
- getJSON()-如何删除数据参数前面的AND符号
- 从json数组中删除数据
- Knockoutjs:只在文本框中写入内容时显示内容,在删除数据时隐藏内容
- 无法删除数据表jquery中的多行
- 根据条件删除数据行
- 在$invalid上添加验证指令并从文本框中删除数据的指令
- 从页面上点击的链接中删除数据,然后移动到下一页&在CasperJS中重复
- 正在删除数据(刷新)WebGL Globe
- d3j :删除数据集
- 轨道:找不到文件“提前输入.js”.直到我删除“数据涡轮链接”
- 如果要使用 JSP 和 Java 脚本删除数据,则需要一个确认框
- 删除数据网格 (Dojo) 和表 (数据网格) 的实例正确显示
- 如何从子元素中删除数据目标和数据切换,或禁止元素触发事件
- 在 d3 中单击画布时删除数据
- JSON 解析或删除数据中的双引号,获取表单模型或 rails 中的数据库
- 如何从 php 文件向 json 文件添加和删除数据
- AngularJS:ng-switch从输入字段中删除数据
- 从嵌套对象中删除数据而不发生突变
- 在挖空.js中删除数据后刷新视图模型