简体   繁体   English

如何在属性值上创建没有双引号的javascript对象

[英]how to create a javascript object without double quotes on the property value

From this data 从这些数据

[{"lat":"-1.325416","lng":"36.669051"},
 {"lat":"-1.392932","l‌​ng":"36.768752"},
 {"l‌​at":"-1.390505","lng‌​":"36.810023"},
 {"lat‌​":"-1.448266","lng":‌​"36.952769"},
 {"lat":‌​"-1.267033","lng":"3‌​7.094882"},
 {"lat":"-‌​1.214605","lng":"37.‌​053978"},
 {"lat":"-1.‌​169516","lng":"36.89‌​5608"}]

I am trying to create a javascript object that looks like this. 我正在尝试创建一个看起来像这样的javascript对象。

 var outerCoords =[
          {lat: -1.325416, lng: 36.669051},
          {lat: -1.392932, lng: 36.768752},
          {lat: -1.390505, lng: 36.810023},
          {lat: -1.448266, lng: 36.952769},
          {lat: -1.267033, lng: 37.094882},
          {lat: -1.214605, lng: 37.053978}, 
          {lat: -1.169516, lng: 36.895608},
          {lat: -1.244058, lng: 36.730391}
        ],

the property value without double quotes. 没有双引号的属性值。 I have first, stringified my json to get a string, then removed the double quotes from the string, then parsed the result with no double quotes. 我有第一个,我的json字符串得到一个字符串,然后从字符串中删除双引号,然后解析结果没有双引号。 Parsing the result does not create an object, it returns a string. 解析结果不会创建对象,它会返回一个字符串。 Please if you can help i'll appreciate. 如果你能帮忙我会很感激。 This is what am doing. 这就是我在做什么。

var str= JSON.stringify(outercords1); 
var x = str.replace (/"/g,'');
var obj= JSON.parse(x);

the value of outercords is: 外线的价值是:

[{"lat":"-1.325416","lng":"36.669051"},{"lat":"-1.392932","lng":"36.768752"},{"lat":"-1.390505","lng":"36.810023"},{"lat":"-1.448266","lng":"36.952769"},{"lat":"-1.267033","lng":"37.094882"},{"lat":"-1.214605","lng":"37.053978"},{"lat":"-1.169516","lng":"36.895608"}] 

Stringified JSON has quoted properties. Stringified JSON具有引用属性。 This is mandatory. 这是强制性的。 So a javascript object in the form 所以表单中的javascript对象

var outerCoords =[
      {lat: -1.325416, lng: 36.669051},
      {lat: -1.392932, lng: 36.768752},
      {lat: -1.390505, lng: 36.810023},
      {lat: -1.448266, lng: 36.952769},
      {lat: -1.267033, lng: 37.094882},
      {lat: -1.214605, lng: 37.053978}, 
      {lat: -1.169516, lng: 36.895608},
      {lat: -1.244058, lng: 36.730391}
    ],

Will be stringified as 将字符串化为

'[{"lat":-1.325416,"lng":36.669051},{"lat":-1.392932,"lng":36.768752},{"lat":-1.390505,"lng":36.810023},{"lat":-1.448266,"lng":36.952769},{"lat":-1.267033,"lng":37.094882},{"lat":-1.214605,"lng":37.053978},{"lat":-1.169516,"lng":36.895608},{"lat":-1.244058,"lng":36.730391}]'

removing the quotes will turn it invalid to be parsed as JSON, so that's why you're getting a string. 删除引号将使其无效以解析为JSON,这就是为什么你得到一个字符串。

On the other hand, performing 另一方面,表演

JSON.parse('[{"lat":-1.325416,"lng":36.669051},{"lat":-1.392932,"lng":36.768752},{"lat":-1.390505,"lng":36.810023},{"lat":-1.448266,"lng":36.952769},{"lat":-1.267033,"lng":37.094882},{"lat":-1.214605,"lng":37.053978},{"lat":-1.169516,"lng":36.895608},{"lat":-1.244058,"lng":36.730391}]');

Will give you an object. 会给你一个对象。 The same object you hand when you begun. 你开始时提到的同一个对象。

Object properties are always casted as strings, so declaring your object as 对象属性始终作为字符串进行转换,因此将对象声明为

var outerCoords =[
  {"lat": -1.325416, "lng": 36.669051},
  {"lat": -1.392932, "lng": 36.768752},
  {"lat": -1.390505, "lng": 36.810023},
  {"lat": -1.448266, "lng": 36.952769},
  {"lat": -1.267033, "lng": 37.094882},
  {"lat": -1.214605, "lng": 37.053978}, 
  {"lat": -1.169516, "lng": 36.895608},
  {"lat": -1.244058, "lng": 36.730391}
];

is the same as declaring it without quotes. 与没有引号的声明相同。

 var outerCoords =[ {lat: -1.325416, lng: 36.669051}, {lat: -1.392932, lng: 36.768752}, {lat: -1.390505, lng: 36.810023}, {lat: -1.448266, lng: 36.952769}, {lat: -1.267033, lng: 37.094882}, {lat: -1.214605, lng: 37.053978}, {lat: -1.169516, lng: 36.895608}, {lat: -1.244058, lng: 36.730391} ] var str= JSON.stringify(outerCoords); //var x = str.replace (/"/g,''); var obj= JSON.parse(str); console.log(obj); 

好吧,我认为不要使用stringify来解决你的问题。

You could iterating the array and the objects and convert all stringed numbers to number. 您可以迭代数组和对象,并将所有带有数字的数字转换为数字。

The keys have, according to the JSON , always double quotes. 根据JSON ,密钥总是双引号。

  var outerCoords = [{ "lat": "-1.325416", "lng": "36.669051" }, { "lat": "-1.392932", "lng": "36.768752" }, { "lat": "-1.390505", "lng": "36.810023" }, { "lat": "-1.448266", "lng": "36.952769" }, { "lat": "-1.267033", "lng": "37.094882" }, { "lat": "-1.214605", "lng": "37.053978" }, { "lat": "-1.169516", "lng": "36.895608" }]; outerCoords.forEach(function (o) { Object.keys(o).forEach(function (k) { if (isFinite(o[k])) { o[k] = +o[k]; } }); }); console.log(outerCoords); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

You can use JSON.parse with a custom reviver function to manipulate your elements. 您可以使用JSON.parse和自定义reviver函数来操作元素。

 var outderCoords1=[{"lat":"-1.325416","lng":"36.669051"}, {"lat":"-1.392932","lng":"36.768752"}]; var outerCoords = JSON.parse(JSON.stringify(outderCoords1), function(name, value) { if(!(typeof value.replace === "undefined")){ var v= parseFloat(value.replace(/"/g, "")); alert(typeof(v)); } }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM