想要使用javascript从json字符串中的项目中删除单引号 [英] Want to remove single quote from items inside json string using javascript

查看:474
本文介绍了想要使用javascript从json字符串中的项目中删除单引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSON字符串,其中的项目以单引号出现,但我想从javaScript中的项目中删除这些单引号。



以下字符串给出 - < br $>


I have a JSON string in which items coming in single quote but i want to remove these single quote from items from javaScript.

below string is given-

" [{
       'id': '0.0',
       'parent': '',
       'name': 'National'
   }, {
       'id': '1.1',
       'parent': '0.0',
       'name': 'C1',
       'value': 4286
   }, {
       'id': '1.2',
       'parent': '0.0',
       'name': 'C2',
       'value': 4258
   }, {
       'id': '1.3',
       'parent': '0.0',
       'name': 'C3',
       'value': 3150
   }, {
       'id': '1.4',
       'parent': '0.0',
       'name': 'C4'
   }, {
       'id': '1.5',
       'parent': '0.0',
       'name': 'C5',
       'value': 2958
   }, {
       'id': '1.6',
       'parent': '0.0',
       'name': 'C6',
       'value': 6295
   }, {
       'id': '1.7',
       'parent': '0.0',
       'name': 'C7',
       'value': 8629
   }, {
       'id': '1.8',
       'parent': '0.0',
       'name': 'C8',
       'value': 3958
   }
   ] "


I want below output ----


" [{
       id: 0.0,
       parent: ,
       name: National
   }, {
       id: 1.1,
       parent: 0.0,
       name: C1,
       value: 4286
   }, {
       id: 1.2,
       parent: 0.0,
       name: C2,
       value: 4258
   }, {
       id: 1.3,
       parent: 0.0,
       name: C3,
       value: 3150
   }, {
       id: 1.4,
       parent: 0.0,
       name: C4
   }, {
       id: 1.5,
       parent: 0.0,
       name: C5,
       value: 2958
   }, {
       id: 1.6,
       parent: 0.0,
       name: C6,
       value: 6295
   }, {
       id: 1.7,
       parent: 0.0,
       name: C7,
       value: 8629
   }, {
       id: 1.8,
       parent: 0.0,
       name: C8,
       value: 3958
   }
   ] "



请建议。 。



我尝试了什么:



我试过用javascript但是没有得到理想的结果。


Please suggest..

What I have tried:

I have tried using javascript but don't get desired result.

推荐答案

Hello Suneel,



请使用下面的代码删除来自字符串的所有单引号。

Hello Suneel,

Please use below code to remove all single quote from the string.
var str = "[{'id': '0.0','parent': 'none','name': 'National'}]";
str.replace(/\'/gi,'')



这是输出


Here is the output

"[{id: 0.0,parent: none,name: National}]"





问候,

Imdadhusen



Regards,
Imdadhusen


无论如何,您没有发布代码来删除单引号。在这里我建议



Anyway, you didn't post the code to remove the single quote. Here what I would suggest

var x = " [{'id': '0.0','parent': '','name': 'National'},{'id': '1.1', 'parent': '0.0','name': 'C1','value': 4286}] ";

var y = x.replace(/'/g, "");





输出:

[ {id:0.0,parent :, name:National},{id:1.1,parent:0.0,name:C1,value:4286}]



Output:
[{id: 0.0,parent: ,name: National},{id: 1.1, parent: 0.0,name: C1,value: 4286}]


string jsonstr = @" [{
      'id': '0.0',
      'parent': '',
      'name': 'National'
  }, {
      'id': '1.1',
      'parent': '0.0',
      'name': 'C1',
      'value': 4286
  }, {
      'id': '1.2',
      'parent': '0.0',
      'name': 'C2',
      'value': 4258
  }, {
      'id': '1.3',
      'parent': '0.0',
      'name': 'C3',
      'value': 3150
  }, {
      'id': '1.4',
      'parent': '0.0',
      'name': 'C4'
  }, {
      'id': '1.5',
      'parent': '0.0',
      'name': 'C5',
      'value': 2958
  }, {
      'id': '1.6',
      'parent': '0.0',
      'name': 'C6',
      'value': 6295
  }, {
      'id': '1.7',
      'parent': '0.0',
      'name': 'C7',
      'value': 8629
  }, {
      'id': '1.8',
      'parent': '0.0',
      'name': 'C8',
      'value': 3958
  }
  ] ";

           jsonstr = jsonstr.Replace("'", "");


这篇关于想要使用javascript从json字符串中的项目中删除单引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆