简体   繁体   English

如何从Google Spreadsheet JSON Feed获取特定属性

[英]How to get specific property from Google Spreadsheet JSON feed

I am reading JSON from a Google Spreadsheet and need help getting to the text within entry.content.$t. 我正在从Google Spreadsheet中读取JSON,并且需要帮助进入entry.content。$ t中的文本。 The text is a column named "description" in the spreadsheet. 文本是电子表格中名为“描述”的列。 The feed for the spreadsheet is (removed) 电子表格的供稿已(已删除)

So far, my script is 到目前为止,我的脚本是

function listChapters(root) {
    var feed = root.feed;
    var entries = feed.entry || [];
    var html = ['<ul>'];
    for (var i = 0; i < entries.length; ++i) {
        var chlist = entries[i];
        var title = (chlist.title.type == 'html') ? chlist.title.$t : escape(chlist.title.$t);
        var chapters = chlist.content.$t;
        html.push('<li>', chapters, '</li>');
    }
    html.push('</ul>');
    document.getElementById("chapterlist").innerHTML = html.join("");
}

The question is - How do I read "description" from $t to place in the var chapters? 问题是-如何从$ t读取“描述”以放置在var章中?

The text within chlist.content.$t is almost, but not quite, properly formatted JSON. chlist.content.$t的文本几乎但不是完全正确地格式化为JSON。 Since it's not properly formatted, you cannot use JSON.parse() to create an object that you could then get a description property from. 由于格式不正确,因此无法使用JSON.parse()创建对象,然后可以从该对象获取description属性。

Here's a brute-force approach that will extract the description , used in place of the original html.push('<li>', chapters, '</li>'); 这是一种蛮力方法,它将提取description ,以代替原始的html.push('<li>', chapters, '</li>'); :

// Get the text between 'description: ' and 'Chapter website:'
var descStart = chapters.indexOf('description:')+13;  //+length of 'description: '
var descEnd = chapters.indexOf('Chapter website:');
var description = chapters.substring(descStart,descEnd);

html.push('<li>', description, '</li>');

Tested with this, checking results in debugger: 经过测试,在调试器中检查结果:

function test() {
  var url = '---URL---';
  var result = UrlFetchApp.fetch(url);
  var text = result.getContentText();
  var bodytext = Xml.parse(text,true).html.body.getText();
  var root = JSON.parse(bodytext);
  listChapters(root);
}

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

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