简体   繁体   English

javascript将预格式化的数组字符串转换为js中可用的数组

[英]javascript convert a pre-formatted array string to a usable array in js

I have what I hope is a simple question, though it may be that I am going about it the wrong way from the start, so let me explain. 我有一个简单的问题,尽管可能是从一开始就以错误的方式进行,所以让我解释一下。

I am drawing polygons on a google map as the user drags/zooms the map. 我在用户拖动/缩放地图时在Google地图上绘制多边形。 So on dragend/zoom change I fire off a javascript method that calls a webAPI that returns a "string" that is pre-formatted to javascript array format (in the past we used this using <%= getdata() %> with no problems. 因此,在dragend / zoom更改中,我触发了一个JavaScript方法,该方法调用了一个webAPI,该API返回一个“字符串”,该字符串已预先格式化为javascript数组格式(过去我们使用<%= getdata()%>进行了此操作。

The string is usually quite long, but a sample with only 3 points is here: 该字符串通常很长,但是这里只有3分的示例:

"{lat: 36.206133, lng: -95.81533},{lat: 36.222298, lng: -95.814961},{lat: 36.22445, lng: -95.81527}"

Now, I see exactly what is happening. 现在,我确切地了解发生了什么。 I just do not know how to fix it. 我只是不知道如何解决它。 Its treating it as a string, so when I try: 将其视为字符串,因此当我尝试时:

var array = [ data ]; var array = [data]; (where data is the string returned above), it fails as it is not an array of coordinate objects as expected, but a single member array of a string. (其中data是上面返回的字符串),它失败了,因为它不是预期的坐标对象数组,而是字符串的单个成员数组。

So... a question with 2 parts: 1, is there a better way to do this, and if not, how do I cast that "string" as an array of coordinate objects just as if that was typed directly in to javascript? 因此,问题由两部分组成:1,是否有更好的方法来执行此操作;如果没有,我如何将“字符串”转换为坐标对象数组,就像直接在javascript中键入一样?

Thanks, 谢谢,

As @jmesolomon already commented, you could/should use JSON.parse for this. 正如@jmesolomon已经评论的那样,您可以/应该为此使用JSON.parse

However, this would require the input string to be of a valid JSON format. 但是,这将要求输入字符串具有有效的JSON格式。 Currently that's not the case because of two things: 目前由于两种原因,情况并非如此:

1) The object's keys aren't of the type 'string'. 1)对象的键不是'string'类型的。 Change: Wrap the keys with double quotation marks. 更改:用双引号引起来的密钥。

2) The response actually is a comma seperated list of objects, but not defined as an array because there's no matching '[' + ']' tags. 2)响应实际上是用逗号分隔的对象列表,但未定义为数组,因为没有匹配的'['+']'标记。 Change: Make sure the WebAPI to format the response as an actual javascript array by adding '[' + ']' tags. 更改:确保WebAPI通过添加'['+']'标签将响应格式化为实际的javascript数组。

Example: 例:

 var originalInput = "{lat: 36.206133, lng: -95.81533},{lat: 36.222298, lng: -95.814961},{lat: 36.22445, lng: -95.81527}"; var workingInput ='{"lat": 36.206133, "lng": -95.81533},{"lat": 36.222298, "lng": -95.814961},{"lat": 36.22445, "lng": -95.81527}'; var betterInput ='[{"lat": 36.206133, "lng": -95.81533},{"lat": 36.222298, "lng": -95.814961},{"lat": 36.22445, "lng": -95.81527}]'; console.log("workingInput", JSON.parse("[" + workingInput + "]")); console.log("betterInput", JSON.parse(betterInput)); 

quick and dirty: 快速又肮脏:

eval("["+data+"]");

Of course this is a security issue if somebody else injects code in your data-String. 当然,如果其他人在您的数据字符串中注入代码,这将是一个安全问题。

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

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