从Google电子表格中解析JSON [英] Parse JSON from Google Spreadsheet

查看:110
本文介绍了从Google电子表格中解析JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑: entry.content。$ t 是访问单个单元格的错误字段。 entry.gsx $ [单元格列标题] 是正确的方法。道歉和感谢,帮助解决这个问题。



原始问题:

我试图解析 Google电子表格中的JSON数据。问题是,entries字段返回一个字符串,它是整个电子表格的一行 - 但显示为格式错误的对象。其他人如何解析这些数据?以下是内容节点的样子:

 content:
{
type: text,
$ t:位置:加利福尼亚州旧金山市巴伦西亚街780号邮编94110,电话号码:(555)555-5555,
网址:http:// www。 780cafe.com,
纬度:37.760505,
经度:-122.421447

仔细查看, $ t 字段会返回整个字符串,这是Google电子表格中的一行。所以 entry.content。$ t 返回一个字符串: location:780 Valencia St San Francisco,CA 94110,电话号码:(555)555-5555。 ..



进一步加剧此问题的原因是电子表格中的一些单元格带有逗号(如地址),这些逗号不会被转义或引用。就像

  jQuery.parseJSON(entry.content。$ t)

  eval('('+ entry.content 。$ t +')')

会引发错误。我想正则表达式是一个选项,但我希望其他人可能已经用更优雅的方式解决了这个问题。感谢您的帮助!

解决方案

最近,我遇到了同样的问题。



要解析 $ t 的内容,您可以使用此RegEx:

  / (\ w +):(。+?(?=(?:, \ w +:| $)))/ mgi 

它将返回键值对。



JavaScript示例:

  var currentPropertiesText = jsonEntry ['content'] ['$ t']; 

// var propsArray = currentPropertiesText.split(,);
var re = /(\w +):(。+?(?=(?:, \ w +:| $)))/ mgi;
var propsArray = re.exec(currentPropertiesText)
var props = {};

while(propsArray!= null){

var propName = propsArray [1];
var propValue = propsArray [2];

道具[propName] = propValue;

propsArray = re.exec(currentPropertiesText);
}

这应该有所帮助: - )


EDIT: entry.content.$t is the wrong field to access individual cells. entry.gsx$[cell column header] is the correct method. Apologies and thanks for helping to solve this.

Original question:

I'm trying to parse JSON data from a Google Spreadsheet. The problem is, the entries field returns a string that is an entire row of the spreadsheet—but appears as a malformed object. How are other people parsing this data? Here is what the content node looks like:

"content":
{
    "type"   :"text",
    "$t"     :"location: 780 Valencia St San Francisco, CA 94110,
               phonenumber: (555) 555-5555,
               website: http://www.780cafe.com,
               latitude: 37.760505,
               longitude: -122.421447"
},

Look carefully, the $t field returns an entire string which is a row in the Google spreadsheet. So entry.content.$t returns a string: location: 780 Valencia St San Francisco, CA 94110, phonenumber: (555) 555-5555...

Further exacerbating this issue is that some of the cells in the spreadsheet have commas (like addresses) which aren't escaped or quoted. Something like

jQuery.parseJSON(entry.content.$t)

or

eval('('+ entry.content.$t + ')')

throws an error. I suppose regex is an option, but I'm hoping others may have solved this in a more elegant way. Thanks for the help!

解决方案

Just recently, I had the very same problem.

To parse content of $t, you can use this RegEx:

/(\w+): (.+?(?=(?:, \w+:|$)))/mgi

it will return pairs of key-value.

JavaScript example:

    var currentPropertiesText = jsonEntry['content']['$t'];

    // var propsArray = currentPropertiesText.split(", ");
    var re = /(\w+): (.+?(?=(?:, \w+:|$)))/mgi;
    var propsArray = re.exec(currentPropertiesText)
    var props = {};

    while (propsArray != null) {

        var propName = propsArray[1];
        var propValue = propsArray[2];

        props[propName] = propValue;

        propsArray = re.exec(currentPropertiesText);
    }

That should help :-)

这篇关于从Google电子表格中解析JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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