Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse JSON foreach with JS, shows HTML list

Tags:

I am currently trying to parse a JSON with JavaScript. My issue is that I'd like the output to look like this:

<li>AppName1</li>
<li>AppName2</li>
<!-- and so on... -->

However it just does not work and I don't know how to achieve that. This is the object deserialized from the JSON response:

{
  "data": [{
    "AppId": 1,
    "AppName": "AppName1",
    "AppSize": "2.1"
  }, {
    "AppId": 2,
    "AppName": "AppName2",
    "AppSize": ""
  }]
}

This is my .js file:

var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
  if (this.readyState == 4 && this.status == 200) {
    var myObj = JSON.parse(this.responseText);
    document.getElementById("test").innerHTML = myObj.AppName;
  }
};
xmlhttp.open("GET", "json.json", true);
xmlhttp.send();

This is in my HTML file

<p id="test"></p>

Any help would be appreciated as I really cannot seem to understand this a single bit. Thank you so much!

like image 609
Leon Mlaiel Avatar asked Feb 15 '19 12:02

Leon Mlaiel


2 Answers

Firstly note that you can only have li elements as children of <ul> or <ol>, so the p element needs to be changed.

The AppName property is part of the objects within data, so you will need to either loop through them:

myObj.data.forEach(function(o) {
   document.getElementById("test").innerHTML += '<li>' + o.AppName + '</li>';
}

Or access them, individually by index:

document.getElementById("test").innerHTML = '<li>' + myObj.data[0].AppName + '</li>'; // first item only

var myObj = {
  "data": [{
    "AppId": 3,
    "AppName": "AnimojiStudio",
    "AppSlug": "animojistudio",
    "AppIcon": "https:\/\/img.lmdinteractive.pro\/icons\/animojistudio.png",
    "AppUrl": "https:\/\/ipa.lmdinteractive.pro\/ipa\/appstore\/animojistudio.ipa",
    "AppVersion": "1.2.2",
    "AppSize": "2.1"
  }, {
    "AppId": 2,
    "AppName": "Cute Cut Pro",
    "AppSlug": "cute-cut-pro",
    "AppIcon": "http:\/\/is2.mzstatic.com\/image\/thumb\/Purple118\/v4\/03\/70\/69\/03706968-2399-a1d8-e7c4-12897394ead9\/source\/512x512bb.jpg",
    "AppUrl": "https:\/\/ipa.lmdinteractive.pro\/ipa\/appstore\/cutecutpro.ipa",
    "AppVersion": "",
    "AppSize": ""
  }]
}

document.getElementById("test").innerHTML = '<li>' + myObj.data[0].AppName + '</li>';
<ul id="test"><li>
like image 170
Rory McCrossan Avatar answered Nov 15 '22 07:11

Rory McCrossan


If you just want a list of the AppName properties, you could do something like the below with jQuery. See the comments in the code for details:

// Below is the JSON string from the OP's link
let json = '{"data":[{"AppId":3,"AppName":"AnimojiStudio","AppSlug":"animojistudio","AppIcon":"https:\/\/img.lmdinteractive.pro\/icons\/animojistudio.png","AppUrl":"https:\/\/ipa.lmdinteractive.pro\/ipa\/appstore\/animojistudio.ipa","AppVersion":"1.2.2","AppSize":"2.1"},{"AppId":2,"AppName":"Cute Cut Pro","AppSlug":"cute-cut-pro","AppIcon":"http:\/\/is2.mzstatic.com\/image\/thumb\/Purple118\/v4\/03\/70\/69\/03706968-2399-a1d8-e7c4-12897394ead9\/source\/512x512bb.jpg","AppUrl":"https:\/\/ipa.lmdinteractive.pro\/ipa\/appstore\/cutecutpro.ipa","AppVersion":"","AppSize":""}]}';

// Parse the JSON string into a JS object
json = JSON.parse(json);
let html = "";

// Loop over the object and append a list item for each AppName property.
$.each(json.data, function (index, item) {
    html += "<li>" + item.AppName + "</li>";
});

// Append the list to the div.
$("#container").append(html);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.3/jquery.min.js"></script>
<div id="container"></div>
like image 24
JoshG Avatar answered Nov 15 '22 05:11

JoshG