How to work with JSON?

Hi all.

I have a test json file with the data I need to write to localStorage and then take from there and carry out the manipulation.

Downloaded the file and saved in localStorage:
$.getJSON( '../json/dataAccount.json', function( data ) {
 localStorage.setItem('accounts', JSON.stringify(data))
 });


Then you want to the variable to load and it has to take and record.
var DATA = JSON.parse(localStorage.getItem('accounts'));


Different tried to record the object, but it is not as I need.
5afc1fd2d8814435551287.jpeg

A piece of json:
[{
 "Acmeindustries": {
 "domain": ["com Commercial organization", "net Network", "Organization org"],
 "ipaddress": ["12.12.162.0", "165.12.21.170"]
}
},
 "Unitedindustrial": {
 "domain": ["Commercial net ", "net Network", "Organization org"],
 "ipaddress": ["12.12.162.0", "165.12.21.170"]
}
}
}]


Please tell me how to write to a variable.
June 5th 19 at 21:39
1 answer
June 5th 19 at 21:41
Solution
not very clear, but what you need

var a = [
{
 "Acmeindustries": {
 "domain": [
 "com Commercial organization",
 "net Network",
 "Organization org"
],
 "ipaddress": [
"12.12.162.0",
"165.12.21.170"
]
}
},
{
 "Unitedindustrial": {
 "domain": [
 "Commercial net ",
 "net Network",
 "Organization org"
],
 "ipaddress": [
"12.12.162.0",
"165.12.21.170"
]
}
}
]

console.log(a) // [{...}, {...}]
localStorage.setItem('dt', JSON.stringify(a))

var b = JSON.parse(localStorage.getItem('dt'))
console.log(b) // [{...}, {...}]
I have a lot of data, so I recorded in a separate file dataAccount.json. I need to take the data as a regular object. For example b.Acmeindustries.domain, and the like - lily.Abshire82 commented on June 5th 19 at 21:44
Generally out what to do

var DATA = JSON.parse(localStorage.getItem('accounts')),
 DATAObj = DATA[0],
console.log(DATAObj.Acmeindustries);


DATAObj and after to refer to. But it seems to me that this is not very nice. If anyone knows how to do it differently, we are waiting for comments. - lily.Abshire82 commented on June 5th 19 at 21:47
so. Stop
Now you just took the first element in the array and work with it

you in the end? Formulate question is not clear. what is clear is that "In different ways tried to record the object, but it is not as I need." - orrin_Hilpert commented on June 5th 19 at 21:50
you need to localStorage to store each object separately or to store in one array, but then to work with them separately? - orrin_Hilpert commented on June 5th 19 at 21:53
Just on another note, I do not understand how to do it. I need to store all data in one array. To take this data, save to variable if you want to edit, and record again.

Sorry if confused. - lily.Abshire82 commented on June 5th 19 at 21:56
so, okay
the code I gave in the answer saves an array in localStorage and returns there is also an array

simple take it in two methods, like saveToLs(data) and loadFromLs() and work with them - orrin_Hilpert commented on June 5th 19 at 21:59
and what methods? and how to use them. Google, neither of which has given - lily.Abshire82 commented on June 5th 19 at 22:02
not-not, they are not. You need to create for ease of operation

like that:
saveToLS function(dt){
 localStorage.setItem('data', JSON.stringify(dt))
}

loadFromLS function(){
 return JSON.parse(localStorage.getItem('data'))
}
var data = [
{
 "Acmeindustries": {
 "domain": [
 "com Commercial organization",
 "net Network",
],
 "ipaddress": [
"12.12.162.0",
"165.13.21.170"
]
}
},
{
 "Unitedindustrial": {
 "domain": [
 "Commercial net ",
 "net Network",
 "Organization org"
],
 "ipaddress": [
"12.12.162.0",
"165.12.21.179"
]
}
}
]



function saveToLS(data);

loadFromLS().forEach(acc => {
 var name = Object.keys(acc)[0]
 var domains = acc[name].domain
 var addresses = acc[name].ipaddress

console.log(name) 
console.log(domains)
console.log(addresses)
})
- orrin_Hilpert commented on June 5th 19 at 22:05
thank you) - lily.Abshire82 commented on June 5th 19 at 22:08

Find more questions by tags JSON