Parsing Json using jquery - not getting value - javascript

I am getting following values from server
{"status":{"message":"success","code":200},"data":[{"sent":"test","category":"Appo","experience_time":"2014-10-07","sent_id":4501922,"categoryId":4011,"score":"Negative","feature":"emp","op":"challenges"}]}
I need to get the value of sent,category,experience_time,sent_id,score,feature,op etc
I have tried following so far.But not getting any value.
var result = jQuery.parseJSON(data);
$.each(result, function(index, value) {
alert(value.score);
});

try this,
var jsonString = '{"status":{"message":"success","code":200},"data":[{"sent":"test","category":"Appo","experience_time":"2014-10-07","sent_id":4501922,"categoryId":4011,"score":"Negative","feature":"emp","op":"challenges"}]}';
var result = jQuery.parseJSON(jsonString);
$.each(result.data, function(index, value) {
alert(value.score);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Related

how can i filtering data from Firebase rt-db using javascript

I need to print in console value of key temp but no things displayed
var database=firebase.database();
var ratingRef = firebase.database().ref("sensors/temp/");
ratingRef.orderByValue().on("value", function(data) {
data.forEach(function(data) {
console.log( data.val());
});
});
There's no need for using orderByValue. You have direct reference to the temp child so the data (which is a DataSnapshot) contain the value of temp only.
var ratingRef = firebase.database().ref("sensors/temp");
ratingRef.on("value", function(data) {
console.log(`Temp: ${data.val()}`)
});

getting json data from tree and feeding through to associative array

I'm trying to read a JSON API and parse the data into an associative array but I can't seem to get it to work. I must be doing something wrong.
Here is my code:
var matchedValues = {};
$.getJSON(url ,function(data) {
$.each(data, function() {
var value = this["value"];
var climb = this["climb"];
matchedValues[value] = climb;
});
});
console.log(matchedValues); //Outputs Object{}
Any ideas? I don't think I am console logging it correctly or maybe I'm doing something wrong?
Thanks
matchedValues is an Object not an array. try something likmatchedValues['your_key'] to get the value.
I assume that the data coming back from the ajax call is a JSON string, which you wish to parse. If that’s the case, then the problem is already solved using JavaScript’s JSON object:
var matchedValues = {};
$.getJSON(url ,function(data) {
matchedValues=JSON.parse(data);
});
console.log(JSON.stringfy(matchedValues)); //Outputs Object{}
As you see in the above example, you have to reverse the process in order to print it out.
See JSON MDN Article
Try this approach from jquery documentation
EDIT:
$.getJSON(url, function(data) {
var matchedValues = {};
$.each(data, function(key, val) {
items[key] = val;
});
If I understood your question, then your data is an Array of objects, and you want to consolidate them in a big object.
If got it right, then use this approach:
var matchedValues = {};
$.getJSON("ajax/test.json", function(data) {
for (var i = 0; i < data.length; i++) {
for (key in data[i]) {
matchedValues[key] = data[i][key];
}
}
console.log(matchedValues); //this should print your object.
});
The data objects must have different keys or they will be overlapsed.

parse value from a php jsonstring in jquery

I am getting following json from php
{"status":{"message":"success","undeleted":[],"code":200}}
{"status":{"message":"success","undeleted":[{"id":7844118,"error":"This Document already Published"}],"code":200}}
I just want to check 'undeleted' is empty or not in jquery.If not empty,I need to take every ids and its error message from 'undeleted'.I am not getting an idea how to do this .I will get only one at a time.
Thinking about something like this
var result = jQuery.parseJSON(data);
$.each(result.data, function(index, value) {
});
I have done something like this but not getting desired answer
var result = jQuery.parseJSON(data);
$.each(result.status, function(index, value) {
alert(value);
});
Here is how to do it:
http://jsfiddle.net/2zLby4r8/3/
var result = jQuery.parseJSON('{"status":{"message":"success","undeleted":[{"id":7844118,"error":"This Document already Published"},{"id":999999,"error":"New Errr"}],"code":200}}');
if (result.status.undeleted.length > 0) {
$.each(result.status.undeleted, function(index, value) {
alert("ID: " + value.id);
alert("Error: " + value.error);
});
} else {
alert("blank");
}

jQuery - put html in array for ajax query

I am trying to put some HTML in an array for an ajax query, but when I see array in console then there is only first line of the html. why ? is there a proper way to do this?
My code
var data = new Array();
$('.get_html').each(function() {
var html = $(this).html();
data.push(html);
});
console.log(data);
You're logging the variable content instead of the data array you're pushing html too.
console.log(data);
Change variable content to data in console.log
var data = new Array();
$('.get_html').each(function() {
var html = $(this).html();
data.push(html);
});
console.log(data);
JSFIDDLE
You need to use data instead of content, Also you can use .map() to do this
var data = $('.get_html').map(function () {
return $(this).html();//return this.innerHTML
}).get();
console.log(data);
Demo: Fiddle

Get data from Json using jquery

I have the following json data, which is located in the url http://localhost/stock/index.php/masterfiles/itemgriddata
[{"name":"asdasd","code":"123","id":"1","unit":"Nos","purprice":"25000"},{"name":"Item2","code":"1235","id":"2","unit":"Nos","purprice":"0"}]
I want to get the value of name and code where id equal to 1, and store them as variables using jquery .
I know there is a method like,
$.getJSON('http://localhost/stock/index.php/masterfiles/itemgriddata', function(data) {
console.log(data);
});
but I don't know how to implement it. Can any one help me find the solution?
$.getJSON('http://localhost/stock/index.php/masterfiles/itemgriddata', function(data) {
$.each(data, function(index, val) {
if( val.id == '1'){
var name = val.name,
code = val.code,
unit = val.unit,
purprice = val.purprice,
id = val.id;
}
})
});
But if you want to store all result in array then:
var dataarr = [];
$.getJSON('http://localhost/stock/index.php/masterfiles/itemgriddata', function(data) {
$.each(data, function(index, val) {
if( val.id == '1'){
dataarr.push(
val.name,
val.code,
val.unit,
val.purprice,
val.id;
);
})
});
For more more detail see doco.
You can iterate over the array and check for the object which has id "1" as
arr = [{"name":"asdasd","code":"123","id":"1","unit":"Nos","purprice":"25000"},{"name":"Item2","code":"1235","id":"2","unit":"Nos","purprice":"0"}]
$.each(arr, function(i,ele){
if (ele.id =="1"){
alert(ele.name)
alert(ele.code)
}
});
You can try something like:
$.getJSON('http://localhost/stock/index.php/masterfiles/itemgriddata', function(data) {
var results = [];
$.each(data, function(key, val) {
if (val.id === "1") {
results.push(val.name);
results.push(val.code);
}
});
// Then you can do whatever you want to do with results
});

Categories

Resources