首页 > 解决方案 > 我将如何通过 jQuery 解析多个或更多对象?

问题描述

像这样

► add language identifier to highlight code

```python
def function(foo):
    print(foo)

► 在段落之间放置返回

► 对于换行符,在末尾添加 2 个空格

斜体粗体

► 代码缩进 4 个空格

► 反引号转义like _so_

► 通过在行首放置 > 来引用

► 建立链接(尽可能使用 https https://example.com 示例

标签: javascriptjqueryhtml

解决方案


正如@Pointy 所提到的,您的代码中有多个语法错误(访问obj数组时)。

但是即使在您修复了这些语法错误之后它也无法工作的原因是您的 API 调用的结果是一个字符串,您需要使用JSON.parse().

$.get('https://raw.githubusercontent.com/danielhoset27/test1/master/C2RReleaseData.json', function(obj) {
  // Parse the received json
  const result = JSON.parse(obj);
  // Fix the syntax errors
  document.writeln(result[0].FFN + " : " + result[0].AvailableBuild);
  // Add a line break
  document.write('<br />')
  // Fix the syntax errors again
  document.writeln(result[1].FFN + " : " + result[1].AvailableBuild);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

还要考虑使用Fetch API是您的目标浏览器支持它。

const appendItem = item => document.body.innerHTML += `<p>${item.FFN} : ${item.AvailableBuild}</p>`;

fetch('https://raw.githubusercontent.com/danielhoset27/test1/master/C2RReleaseData.json').then(response => {
  response.json().then(result => {
    appendItem(result[0]);
    appendItem(result[1]);
  });
});


推荐阅读