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.
Related
I need to load multiple different JSON files.Now I use many AJAX to load them one by one, according to a list of their file name. But if I add a new JSON, I should change the list by myself. How can I load JSON files with code automatically?
solution 1:
A possible solution is to create a JSON file that contains all file names. Then you just have to update the JSON file instead of the list.
solution 2:
In the case where the file names are like file1.json, file2.json, ..., you can try to get them with a loop and leave it at the first AJAX resource not found exception.
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
How to access a javascript object from .js file in C# code behind file .aspx.cs file?
var user={name:"john";age:"12"};
I want to access this user object in C# code behind file.
There are may ways one is you can pass it in query string. Here is a similar question on SO
Use like
str = $.param({name:"john";age:"12"});
and use this str in parameter.
Save individual value to hidden field and then get it in code behind.
I want to convert the plaintext Public Suffix List to JSON object so as to use it in my script. I just want the list of TLD's.
The list is here https://publicsuffix.org/list/effective_tld_names.dat
My initial thought was to regex match the file for suffix list but I don't know how to regex files in javascript. I'm kind of new to javascript.
Anyone having idea, how to achieve this. Thank You
First off, the list doesn't seem to be allowed to do cross-domain, so direct AJAX to the resource isn't possible.
What you can do is have your server load it for you (PHP: file_get_contents, JS: http.get). Then, implement a parser. I'm not familiar with the format of the file, but you can read the file line by line, skip blank lines and lines with //. Then load them into an array (PHP: array_push, JS: Array.prototype.push), serialize (PHP: json_encode, JS: JSON.serialize) and ship as JSON to your app.
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