I'm trying to output some values taken from an API (JSON-formatted). And can't find a solution on how to grab the JSON tags and make them turn into HTML. JSON example here.
How do I get that JSON to turn out as DIV-tags, paragraphs and headings?
you need to code that. there are already existing libraries who can help you there: http://json2html.com/
There's no 1-to-1 mapping between JSON and HTML. You will have to do that manually depending on the JSON structure. So for example you could access inidividual elements of your object like this:
result.glossary.GlossDiv.GlossList.GlossEntry.ID
Related
I have a file.json file which contains an array ["number1", "number2"] and I'm trying to add "number3" to that array using javascript. But if I define a variable in js file containing this array and try to edit it, only that variable will be edited, when I'm trying to edit the file itself! It sounds like a simple question, but for some reason I didn't find the answer how to do it. Any help is appreciated!
Get the JSON string from the file.
Turn the JSON string into a javascript object.
Edit the object.
Turn the object back into a JSON string.
Replace the contents of the file with the new JSON string.
For a class project we have to build an online store in 3 parts: html, css and js, the first 2 are done but i'm at a loss about js! We should be able to put the info of one product in a json file, and parse it in html. but tbh i'm not even sure how or where to begin with it, any suggestions are appreciated! Thanks!
First you can start by creating your JSON file that has all the information about the store that you want, then you can use JavaScript to read the data in that file. Finally you can use HTML and some binding to parse and show whatever you want. I can help you out if you post some sample code or a jsfiddle.
I am trying to do a simple task; retrieve JSON using Jquery and display the name to a simple HTML list, usually, the JSON file I deal with is quite straightforward with a format of
[ {a:1,b:2},{a:3,b:4}]
, but this time, the file (hosted on a different website) has a format similar to
[
["John",21,5,"description","some-link"],["Doe",3,6,"description","some-link"],]
with another 100k entries. My goal is to simply display all the names of this file in an HTML list.
Is there a way to retrieve the list and display content such as the name and description in simple HTML ul?
the actual file link is here: https://www.dropbox.com/s/104iutbz95o7wtw/test.json?dl=0 (consider it is actually hosted on a website for example: https://sample.com/test.json)
You need a regex to return a list of the data.
Have a look at this one I made for you: https://regex101.com/r/aDRp6A/1
It would help to know if the data has a strict structure or if it's variable.
EDIT:
Given your sample data I can see yours is just a list of lists. No curly brackets. That's good!
Once you have your json as a string try to split it:
myList = yourJsonObject;
And then you can access the data with:
myList[0][0]
hello you need to build your own parser or use regex, because the format that you mention is not JSON
I'm using Bootstrap Tree to construct a tree control.
You can use a JSON file as the source for tree nodes. All you need to do is this (Source):
<li>JSON Ajax Example</li>
My question is: How can I use a JSON object as the source of the tree instead of a JSON file?
Ultimately, the click event will make a call to the href. It's a post request, passing in any parameters from the data api. So, if the href="mydynamic.cfc" data-method="myjsongenmethod" data-id="someidvalue" data-returnformat="json", then it will perform a jQuery ajax POST, passing a data object of {"method": "myjsongenmethod", "id": "someidvalue", "returnformat": "json"} to your server-side temmplate of "mydynamic.cfc" (or whatever other server-side you want). It will expect your return format to be like what you see in that example json doc.
FYI, this is a reallllllyyyy early version of this plugin, which is why I haven't created a demo site or documentation yet. I think the entire api may change in the future. I want to keep it simple, and in the same overall fashion as the core Bootstrap components.
I want to load an external file using AJAX GET and then parse it for the relevant information on it leaving out all the comments.
file: stuff.conf
: This is the list
: of colors needed
#5d3939 : nice
#9e1818 : ugly!
#cd7979
#409c81
#6e6f14 : ok...
I want the hex colours in an array.
Please help!
Here you go:
var arr = response.match(/\#[a-f0-9]{6}/gi);
where response is your Ajax response string.
Live demo: http://jsfiddle.net/simevidas/RnPS3/1/
You can write your own JS to parse any type of data format. But, the somewhat standard way to interchange data like this with the least hassle is to put the data in the JSON format with the color values in an array (or whatever format you want them to end up on). You then read the contents of the file into a string variable and then call a JSON parser. The return value of the parser will be an array of color values (if that's how you format the JSON). The latest browsers have JSON parsers built in. For cross-browser compatibility with older browsers, you can either use a parser in one of the common libraries like jQuery or YUI or find code on the web to add just a JSON parser.