用JSON结构书籍内容

时间:2019-03-17 01:22:59

标签: json

我是编程和构建读书应用程序的新手。 在此应用中,我需要记住读者在注销之前阅读过的句子。

我无法做到以下结构:

var lastSentence = Chapter.page.sentence.text

"chapter": [
  {
    "id": 1,
    "page": [
      {
        "id": 1,
        "picture": "City_Building.png",
        "sentence": [
          {
            "id": 1,
            "text": "Lorem ipsum...",
          },
          {
            "id": 2,
            "text": "Somethin else...",
          }
        ]
      }
    ]
  }
]

我想尝试以下方法,但是我不确定它是否正确,我应该尝试使结构尽可能平坦:

"chapter": [
  {
    "1": [
      {
        "page": [
          {
            "1": [
              {
                "picture": "City_Building.png",
                "sentence": [
                  {
                    "1": [
                      {
                        "text": "Lorem ipsum..."
                      }
                    ]
                  },
                  {
                    "2": [
                      {
                        "text": "Something else..."
                      }
                    ]
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  }
]

这将允许我这样做:

var lastSentence = chapter.1.page.1.sentence.2.text

这种结构并不太深入,但我仍在寻找更好的方法。

有什么建议吗?

2 个答案:

答案 0 :(得分:1)

你不能做 In[334]:df_short_interests.append(row_data, ignore_index=True) C:\Users\user\Anaconda3\lib\site-packages\pandas\core\indexes\api.py:107: RuntimeWarning: '<' not supported between instances of 'str' and 'int', sort order is undefined for incomparable objects result = result.union(other) Out[334]: Settlement Date Short Interest ... Days To Cover 0 0 NaN NaN ... NaN 2/15/2019 1 NaN NaN ... NaN 39,903,215 2 NaN NaN ... NaN 26,937,971 3 NaN NaN ... NaN 1.481300 因为结构中的var lastSentence = chapter.page.sentence.text是一个数组,所以当您要访问sentence属性时,需要指定索引:text

如果要从最新句子中获取文本,可以使用array的chapter.page.sentence[index].text属性。

.length

请注意,我之所以使用var lastSentence = chapter.page.sentence[chapter.page.sentence.length - 1].text是因为数组索引从0开始,例如数组length - 1有3个元素,索引为[1, 2, 3]

答案 1 :(得分:1)

我认为更好的方法是:

{
    "author": "No Name",
    "title": "Wonderful Journey",
    "publishedDate": "17-03-2019",
    "country": "German",
    "city": "Berlin",
    "publisher": "Publisher Name",
    "chapters": [
        {
            "id": 1,
            "title": "Journey Begin",
            "pages": [
                {
                    "id": 1,
                    "picture": "City_Building.png",
                    "content": "Something else...",
                    "footnote": "Something important..."
                },
                {
                    "id": 2,
                    "picture": "City_Building.png",
                    "content": "Something else...",
                    "footnote": "Something important..."
                }
            ]
        }
    ]
}

您可以将其保存为 book.json 之类的 JSON文件,并像这样从Javascript调用JSON文件:

var book = undefined,
    getBook = new XMLHttpRequest();

getBook.onreadystatechange(function() {
    if(this.readyState == 4 && this.status == 200) {
        book = JSON.parse(this.responseText);

        // get author of book
        console.log(book.author);

        // get content of chapter 1 page 1
        console.log(book.chapters[0].pages[0].content);

        // get footnote of chapter 1 page 2
        console.log(book.chapters[0].pages[1].footnote);
    };

getBook.open('GET', 'book.json');
getBook.send();

有关ajax的信息,您可以遵循此link