I pass from php to js object. For example :
{"3199":{"name":"Ramona RONDEREGGER","mail":"mona.schlaepfer#bluewin.ch","phone":null,"hope":null
,"status":"NEW LEAD"},"3253":{"name":"Olivia
BAUMANN","mail":"oliviazurfluh#gmail.com","phone":null,"hope"
:null,"status":"NEW LEAD"}}
And I want to get data from this object in js (get 3199, 3253 and their data (name,mail ...).
How can I do it?
I try it:
$(data).each(function(key,value){
$(value).each(function(key,value){
console.log(value);
});
});
But id doesn't work
Please, help me to solve this problem
Thanks
Iterate through your object and get data by key. Here is the example of your dataset showed that how can you access the data in your result object.
var data = {"3199":{"name":"Ramona RONDEREGGER","mail":"mona.schlaepfer#bluewin.ch","phone":null,"hope":null
,"status":"NEW LEAD"},
"3253":{"name":"Olivia BAUMANN","mail":"oliviazurfluh#gmail.com","phone":null,"hope"
:null,"status":"NEW LEAD"}};
for(x in data){
// this will print your key
console.log("this is your key " + x);
// this line will print your key object
console.log(data[x]);
//to access internal element
console.log(data[x]['mail']);
}
Specify key like this value["3199"]
var data = {"3199":{"name":"Ramona RONDEREGGER","mail":"mona.schlaepfer#bluewin.ch","phone":null,"hope":null,"status":"NEW LEAD"},"3253":{"name":"Olivia BAUMANN","mail":"oliviazurfluh#gmail.com","phone":null,"hope"
:null,"status":"NEW LEAD"}};
$(data).each(function(key,value){
$(value).each(function(key,value){
console.log(value["3199"]);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Related
How can I change/modify/refresh my localstorage of AngularJS?
My localstorage content is an object json:
//data content: {Id:1,Name:"Juanito"}
localStorageService.set("MyObj",data);
console.log(localStorageService.get("MyObj"));
//SHOW object JSON {Id:1,Name:"Juanito"}
But if I modify a unique key like this:
localStorageService.set("MyObj.Id","otherid"),
It does not work. How do I fix this?
Try retrieving, then updating and re-setting:
var data = {id: 123}
localStorageService.set("MyObj",data);
var dataFromLS = localStorageService.get("MyObj");
dataFromLS.id = 456;
localStorageService.set("MyObj",data);
console.log(localStorageService.get("MyObj"));
When you change/modify your key name, a brand new object will be created/stored in your service.
If you want to change/alter key's data/value, perform intermediate operations and set it with same key.
How can I get the second data of this JSON - I am beta. in jQuery?
{"alpha":["I am alpha"],"beta":["I am beta."]}
You can go through array and search for keys what you want... but dont forget to encode json string
$data= json.encode($input)
foreach($data as $key => $val)
{
if($key=='beta')
do something
else
do something else
}
I hope it help you.
Assuming that your json is in a string variable as:
var data = '{"alpha":["I am alpha"],"beta":["I am beta."]}';
You can parse the string as a json object and then access the content like this:
json = JSON.parse(data);
//or if you strictly need to use jQuery
json = $.parseJSON(data);
alert(json.alpha[0]);
alert(json.beta[0]);
If you don't know which is the json content you can iterate through json object like this:
for (var key in json) {
if (json.hasOwnProperty(key)) {
alert(key + " -> " + json[key]);
}
}
Finally I come up with this
Step 1: Json parsed,
Step 2 : Declared in another string
Step 3 : And then looped inside each entry
obj=JSON.parse(data);
var error_string='';
$.each(obj, function(entry) {
error_string+=obj[entry]+'<br/>';
});
$('#stage').html(error_string);
}
And then i got the values that is inside, Now i can able to get the values inside the object even for n numbers.
I know there are 1 million and 1 questions on this, but I cannot seem to find an answer.
I am receiving data via PHP as such
echo json_encode($result);
From a typical MYSQL query.
I get the result back like this in the console.
[{"id" : "1", "name" : "bob"}]
I am trying to use $.each to iterate through this so I can process my results but I only get errors, undefineds or 0[object Object].. things like that.
My goal is to append each value to a input box (retrieving data from a table to put into an edit box).
$.post('getstuff.php', { id : id } function(data){
$.each(data), function(k,v){
$('input[name= k ]').val(v);
});
});
As you can see i was hoping it was as simple as a key=>value pair but apparantly not. I have tried parsing, stringifiying.. really I am lost at this point. I also cannot tell $.post that I am using JSON because I am using a more arbitrary function, but am just posting that as my example.
Edit
var retrievedData = JSON.parse(data);
$.each(retrievedData, function(k,v){
for (var property in retrievedData) {
if (retrievedData.hasOwnProperty(property)) {
console.log(k);
console.log(v);
console.log(property);
//$('input[name= k ]').val(v);
}
}
});
In your second code sample, retrievedData is an array, which you iterate using jQuery $each...
$.each(retrievedData, function(k, v) {
OK so far. But then you try to iterate retrievedData again like an object, which it isn't. This is why you are getting undefined messages in the console
for (var property in retrievedData) {
if (retrievedData.hasOwnProperty(property)) {
console.log(k);
console.log(v);
console.log(property);
//$('input[name= k ]').val(v);
}
}
On the inner loop you should be iterating v not retrievedData. On each pass of $each v will be an object.Try this:
$.each(retrievedData, function(k,v){
for (var key in v) {
if (v.hasOwnProperty(key)) {
console.log("key: " + key);
console.log("value: " + v[key]);
}
}
});
You should do some type checking that v is an object first and catch any errors.
Use either :
$.ajax({
'dataType' : 'json'
});
Or
$.getJSON
Or if you want to use $.post, just do in your success function :
var good_data = JSON.parse(data);
$.each(good_data, function(k,v) {
$('input[name= k ]').val(v);
});
Answering your question based on your comments on other answer.
My assumption is you are getting data as JSON,if not you need to parse it,for that you can use JSON.parse(string).
Here I'm using Underscore.js
var data=[{"id" : "1", "name" : "bob"}]
$(data).each(function(ind,obj){
var keys=_.keys(obj);
$(keys).each(function(i,ke){
console.log(ke)
console.log(obj[ke]);
})
});
Here is JSFiddle of working code
First you need to define you're expecting JSON in your POST request - http://api.jquery.com/jQuery.post/
Then you need to iterate through the response.
$.post('getstuff.php', { id : id } function(data){
//Assuming you get your response as [{"id" : "1", "name" : "bob"}], this is an array
//you need to iterate through it and get the object and then access the attributes in there
$.each(data), function(item){
$('input[name=' + item.name + ']').val(item.id);
});
}, 'json');
EDIT
If you want to iterate over the properties of the objects returned, you need to put another loop inside the $.each
for (var property in item) {
if (object.hasOwnProperty(property)) {
// do stuff
}
}
More about it here - Iterate through object properties
EDIT 2
To address the solution you've posted. You've used the wrong variable names. Here's a working fiddle - http://jsfiddle.net/EYsA5/
var $log = $('#log');
var data = '[{"id" : "1", "name" : "bob"}]'; //because we're parsing it in the next step
var retrievedData = JSON.parse(data);
for (var parentProp in retrievedData) { //this gets us each object in the array passed to us
if (retrievedData.hasOwnProperty(parentProp)) {
var item = retrievedData[parentProp];
for (var property in item) { //this gives us each property in each object
if (item.hasOwnProperty(property)) {
console.log(item[property]);
$log.prepend("<br>");
$log.prepend("Property name is - " + property);
$log.prepend("<br>");
$log.prepend("Value of property is - " + item[property]);
//do stuff
}
}
}
};
I have the following code to extract values from a JSON response. What I am trying to do is store the data in a similar way to how you would with an associative array in php. Apologies for the code being inefficient. The array comments written down are how I would like it to look in the object.
$.each(responseData, function(k1,v1){
if(k1 == "0"){
$.each(v1, function(k2,v2){
$.each(v2, function(k3, v3){
if(k3 == "val"){
//store in object here
//Array1 = array("time"=>k2, "iVal"=>v3)
console.log(k3 + v3 + k2);
}else{
//Array2 = array("time"=>k2, "aVal"=>v3)
console.log(k3 + v3 + k2);
}
});
});
}
});
So all the information is there but I am not sure how to store each instance for the values in an object. I did try store it like this:
//obj created outside
obj1.date = k2;
obj2.iVal = v3;
But doing this clearly overwrote every time, and only kept the last instance so I am wondering how can I do it so that all values will be stored?
Edit: Added input and output desired.
Input
{"0":{"18.00":{"iVal":85.27,"aVal":0.24},"19.00":{"iVal":85.27,"aVal":0.36},"20.00":{"iVal":0,"aVal":0}}, "success":true}
Desired output
array1 = {"time":"18.00", "iVal":85.27},{"time":"19.00", "iVal":85.27},{"time":"20.00", "iVal":0}
array2 = {"time":"18.00", "aVal":0.24},{"time":"19.00", "aVal":0.36},{"time":"20.00", "aVal":0}
try this :
var g1=[];
var g2=[];
for ( a in o[0])
{
g1.push({time:a , iVal:o[0][a]['iVal']})
g2.push({time:a , aVal:o[0][a]['aVal']})
}
http://jsbin.com/qividoti/3/edit
a json response can be converted back to a js object literal by calling JSON.parse(jsonString) inside the success callback of your ajax call.
from then on there is no need for iterating over that object since you navigate it like any other js object which is can be done in two ways either
the js way -> dot notation
var obj = JSON.parse(jsonStirng);
var value = obj.value;
or like a php array
var value = obj["value"];
I have a JSON object:
var json = {"Mike":1, "Jake":1, "Henry":1};
I am trying to loop through this list to access the names. Currently I am using:
for (var name in json) {
if (json.hasOwnProperty(name)) {
console.log(name);
}
}
But it's not printing the name. Is that the correct way to do it?
jsFiddle: http://jsfiddle.net/bKwYq/
As other people mentioned, this is not JSON, it's just an object.
The hasOwnProperty thing may not really be necessary here also.
var persons = {"Mike":1, "Jake":1, "Henry":1};
for (var name in persons) {
alert(name);
}
This will work in every browser: http://jsfiddle.net/HsNMY/
It can be done by getting the key using Object.keys and then using foreach loop to print to console or display as alert message.
var persons = {"Mike":1, "Jake":1, "Henry":1};
var keysInPerson= Object.keys (persons);
keysInPerson.forEach ((name) => console.log (name));
//Alert
keysInPerson.forEach ((name) => alert (name));
The correct way to PRINT the name is to use document.write instead of console.log, as in this fiddle :
http://jsfiddle.net/jRAna/