Skip to content
Advertisement

How would I parse multiple or more objects via jQuery?

like so

► add language identifier to highlight code

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

► put returns between paragraphs

► for linebreak add 2 spaces at end

italic or bold

► indent code by 4 spaces

► backtick escapes like _so_

► quote by placing > at start of line

► to make links (use https whenever possible) https://example.com example example

Advertisement

Answer

As mentioned by @Pointy there are multiple syntax errors in your code (when accessing the obj array).

But the reason it wouldn’t work even after you fix these syntax errors is that the result of your API call is a string and you need to parse it using 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>

Also consider using Fetch API is your targeted browsers support it.

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]);
  });
});
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement