Access JavaScript array object in PHP [duplicate] - javascript

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 7 years ago.
I have a JSON data about 150 users, i have convert that into java script array, now what i want it to pass this array to PHP, So that i can perform further operations on this array.
var JSONString = '[{'user_id' : "1", "user_name" : "abc", "email":"abc#gmail.com"}, {"user_id":"2", "user_name":"abc", "email":"abc2#gmail.com"}]'
var JSONObject = JSON.parse(JSONString);
for (var key in JSONObject) {
if (JSONObject.hasOwnProperty(key)) {
console.log(JSONObject[key]["user_id"] + ", " + JSONObject[key]["user_name"]);
}
}
now what i need it to pass this array to PHP array variable , that will loop through and perform further operation.
i have seen some solution that display it on html with JavaScript, but i don't need that. i need this array object in php array variable.

Instead of parsing JSON string in JavaScript, pass it directly to PHP and use
PHP function
json_decode($json, true)
it will give you data in array format.

Related

Get value of json file in javascript(nodejs) [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 9 months ago.
So i want to get a value from a json file, but for the life of it, i cant get to understand what to write.
So if someone could help me write this line of code in javascript node, i would be grateful.
I am trying to get the value of "totalCount"
This is from a library called GeoDb just for context.
main.js(NODEjs)
const timezone = require("./TimezonePortugal.json"); //referencing the json
console.log(timezone.metadata); // i dont know what to say afther this
TimezonePortugal.json
{"data":[{"code":"EUR","countryCodes":["PT","YT","IE","AD","IT","AT","RE","AX","BE","BL","SI","SK","SM","CY","DE","LT","LU","LV","MC","ME","MF","EE","MQ","MT","VA","ES","NL","FI","FR","GF","GP","GR","PM"],"symbol":"€"}],"metadata":{"currentOffset":0,"totalCount":1}}
"." means inside.
If you want to get value of totalCount you have to think like this:
timezone.metadata.totalCount = totalCount inside the metada, metadata inside the timezone
For example:
var timezone=
{
"data":[
{
"code":"EUR",
"countryCodes": ["PT","YT",],
"symbol":"€"
}
],
"metadata": {
"currentOffset":0,
"totalCount":1
}
}

receive string from input and convert it to javascript object [duplicate]

This question already has answers here:
Safely turning a JSON string into an object
(28 answers)
Closed 3 years ago.
I want to receive my javascript object such as
{ "user": { "active": true, "dob": '1988-09-11', "group": 14, "department: "business"" } }
from input using a node package such as readline-sync and store it as an object to be able to access to the values seperately using their keys. The readline-sync stores my input as an string I tried to convert it to javascript object using
let obj= JSON.parse(JSON.stringify(stringObj));
but still it is string.
Is it possible to receive a string from the input and convert it object?
JSON.stringify() Receives an object and returns a string. If what you want to do is the inverse (convert a JSON string into an object), you need to use JSON.parse()
Then, what you want is:
let obj = JSON.parse(stringObj);

How to get data from Json Object using javascript [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 4 years ago.
I'm getting data as a json object
var data = JSON.parse(JSON.stringify(data));
console.log(data);
On data console I got this. I want to get key and its value .
{lobby: {…}}
lobby:
8jmb9ca3s04c8el4j5sf0d:"AD8BJBkMKCBoYg_qAAAB"
38cjllj78cx0lic58ujxou:"PX51X9z_M34_9BvtAAAD"
ba8gs1y8779kmakdapxk1:"UowsBDCCsZSpojzPAAAA"
Parse JSON to Object var data = JSON.parse(jsonData);
Use Object.keys(data); to get all keys
Use data.key or data['key'] to get key value.

How to get all the keys in nested json object in java script [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 9 years ago.
Hi below is my json string which is available in javascript
{
"101":{"my":"1", "u":"4", "s":false, "isChecked":true},
"102":{"my":"2", "u":"4", "s":false, "isChecked":false}
.
.
.
"1000":{"my":"999", "u":"888", "s":true, "isChecked":true}
}
Now -for example- I want to get the values of the isChecked property for each key:
"101" "isChecked" is true
"102" "isChecked" is false
...
"1000" "isChecked" is true
Could please you share any useful snippet of code for this scenario?
Use for statement to go through the object:
var data = { /* your data here */ }
for (var key in data) {
console.log(key, data[key].isChecked);
}
http://jsfiddle.net/tZzfb/

parsing json string using json parse method [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 8 years ago.
Hi friends I have a I have a json string as shown below. How to parse the string to get day,min_amount,max_amount values .
[{"day":"1970-01-01","min_amount":"0.00","max_amount":"0.00"},{"day":"1970-01-02","min_amount":"1.00","max_amount":"2.00"}]
Just use JSON.parse. The syntax for accessing a value is simple:
obj = JSON.parse(json)
day = obj[0].day
min_amount = obj[0].day
max_amount = obj[0].day
The great thing about Javascript is how simple it is to use JSON, because JSON is just a serialized version of plain-old javascript hashes, arrays, and scalars.
It's already in object form. SO use this :
var x = [{"day":"1970-01-01","min_amount":"0.00","max_amount":"0.00"},{"day":"1970-01-02","min_amount":"1.00","max_amount":"2.00"}]
jQuery.each(x,function(e){
console.log(x[e])
console.log(x[e].day)
});
Here is the working example : http://jsfiddle.net/u6J8A/
As you may not have noticed, JSON is JavaScript synthax.
<script type="text/javascript">
var data = [
{"day":"1970-01-01","min_amount":"0.00","max_amount":"0.00"},
{"day":"1970-01-02","min_amount":"1.00","max_amount":"2.00"}
];
</script>
Dumping it directly in the JavaScript code is perfectly valid.
But if you are fetching this data at run time and have the information as a string, you can convert it using JSON.parse(string).
The information can be then read from this structure by the variables data[0].day, data[0].min_amount, data[0].max_amount, data[1].day, data[1].min_amount, data[1].max_amount.

Categories

Resources