This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 7 years ago.
I have the following code to get the results of a query. The query results are in the form of JSON.
$.ajax({
url: ..... //url//...,
contentType: "application/json; charset=utf-8",
data: JSON.stringify({
"type": "daysofyear",
"entity": {
"year": "2015"
}
}),
type: "POST",
dataType: "json",
success: function(result) {
if ((result) && (result.isSuccess == true)) {
alert("SUCCESS");
alert(json.entity.day);
}
},
});
The returned JSON is:
{
"isSuccess": true,
"results": [{
"jsonClass": "RuleSuccess",
"message": "Found mapping for year: 2015",
"rule": {
"name": "Year rule",
"metadata": {}
},
"entity": {
"year": "2015",
"month": "December",
"day": "Saturday"
}
}]
}
Im basically trying to extract the value of month, year, day separately and display them alone using
alert(json.entity.day);
Please advice.
Update
var a =JSON.parse('{"isSuccess":true,"results":[{"jsonClass":"RuleSuccess","message":"Found mapping for year: 2015","rule": {"name":"Year rule","metadata":{}}, "entity":{"year":"2015", "month":"December", "day":"Saturday"}}]}') ;
alert(a.results[0].entity.day);
Original
Use
alert(result[0].rule.entity.day);
try this:
obj = JSON.parse(json);
console.log(obj[0].rule.entity.day);
Related
I am sending a request to a Node/Express server using jQuery thats data is a JSON object containing an array:
var data = {
"name": "James Jamesy",
"children": [
{
"name": "Tiny James",
"age": "4"
},
{
"name": "Little James",
"age": "6"
},
{
"name": "Graham",
"age": "8"
}
]
}
var request = $.ajax({
method: 'PUT',
url: apiPath + 'updateuser',
data: data,
dataType: 'json'
});
The request itself is working fine, however the server is reporting the data as:
{
name: 'James Jamesy',
'children[0][name]': 'Little James',
'children[0][age]': '4',
'children[1][name]': 'Medium James',
'children[1][age]': '6',
'children[2][name]': 'Graham',
'children[2][age]': '8'
}
Now I've figured out that I can get my desired result by instead stringifying the children array:
var data = {
"name": "James Jamesy",
"children": JSON.stringify([ ... ])
}
And then JSON.parse()ing it on the server.
However I am hoping someone can explain why the array is converted as it is in the request, and whether I should be handling this a different way? As in this instance converting the single array is fine, but going forward I might have semi-complex objects I'm looking to send to the server.
Thanks in advance!
EDIT: Additionally and strangely(?), if I send the JSON result back as the passed JSON, it works perfectly:
res.json(JSON.parse(req.body.categories));
The browser logs out the object and I can manipulate it perfectly fine.
You weren't passing a JSON string through ajax which is why you couldn't handle the data on the back end.
var data = {
"name": "James Jamesy",
"children": [
{
"name": "Tiny James",
"age": "4"
},
{
"name": "Little James",
"age": "6"
},
{
"name": "Graham",
"age": "8"
}
]
}
var request = $.ajax({
method: 'PUT',
url: apiPath + 'updateuser',
data: JSON.stringify(data),
contentType: 'application/json', // for request
dataType: 'json' // for response
});
I need to filter some information. The information is coming from a JSON parse. I can't make it work. What I want is that the JSON is filtered into the HTML classes. I think I'm stupid
$.ajax({
// Agenda
type: 'POST',
url: 'agendas',
data: {results: 'events'},
dataType: 'json',
cache: false,
success: function (response) {
$('.date, .country, .events').html('');
$.each(response.results, function (index, result) {
if (result.status)
$('.date').append(result.server);
$('.country').append(result.server);
$('.events').append(result.server);
});
}
});
It would be lovely if someone could help me
JSON:
{
"results": [
{
"events": {
"id": 1,
"date": "2022-05-06T00:00:00+00:00",
"description": "test",
"time": "2017-02-03T06:40:00+00:00",
"location": "NL",
"year": "2008",
"event": "Idk"
}
},
{
"events": {
"id": 2,
"date": "2019-04-05T00:00:00+00:00",
"description": "aasdasdasda",
"time": "2017-02-03T15:04:00+00:00",
"location": "asdasdasd",
"year": "0000",
"event": "asdasd"
}
}
]
}
HTML:
<div class="day">
<h2 class="date">Januari 23</h2>
<div class="country-events">
<span class="country">UK</span>
<div class="events">
<span class="event">Conference Amsterdam<br />11:00 CET</span>
<span class="event">Webinar Copenhagen<br />15:00 CET</span>
</div>
</div>
I really can't get my head around this. I know it is not the correct code, I am new to JSON and JavaScript. 4th day currently so please forgive me.
Thank you in advance
UPDATE
This is what it shows currently, the data from the json file needs to go in the specific elements
$.each(response.results, function(index, result) {
console.log(result)
$('.date').append(result.events.date);
$('.date').append('</br>');
$('.country').append(result.events.location);
$('.country').append('</br>');
$('.events').append(result.events.event);
$('.events').append('</br>');
});
is this what you want?
https://plnkr.co/edit/wCYJPXgAPII1mcW4cKw7?p=preview
check this fiddle
This question already has answers here:
Jquery - How to make $.post() use contentType=application/json?
(17 answers)
Closed 6 years ago.
I tried in 2 ways:
saving the json on a var
var dataLog = JSON.stringify( {
"clientId": "1",
"sensor": "Temp",
"dateStart": "2016-09-03 00:00:00",
"dateEnd": "2016-09-03 00:59:59"
} );
$.post(data , {dataLog})
.done(function( data ) {
console.table(data);
});
and adding the same json directly into the data parameter
$.post( url, {
"clientId": "1",
"sensor": "Temp",
"dateStart": "2016-09-03 00:00:00",
"dateEnd": "2016-09-03 00:59:59"
})
.done(function( data ) {
console.log(data);
});
but none of the 2 options works, it is possible or im doing something wrong?
here one example
var promise = $.ajax({
url: url,
type: 'POST',
dataType: "json",
contentType: "application/json; charset=utf-8",
data: dataLog
});
I have a javascript function which generates JSON data at every certain second and then PUT it to a cloud server. Now I don't want to POST in realtime, rather I want to log this data in a buffer and say after n number of data log I will PUT to cloud. For example I want to log 50 data point in 10 second and then with timestamp I will PUT to a server
Now JSON data is passed through var fromDatan. JSON data format is
{"values": [ { "at": "2014-08-17T12:00:00Z", "value": "15" }]}
This is a single instance which is passing through say var fromDatan and being PUT in cloud.
Now I want to log say n number of JSON data. ie.
{ "values": [ { "at": "2014-08-17T12:00:00Z", "value": "15" }, { "at": "2014-08-18T12:00:00Z", "value": "20" }, { "at": "2014-08-19T12:00:00Z", "value": "25" } ] }
And then I will PUT to cloud. This is my PUT code for real time which is working:
$.ajax({
url: "https://abcd.com",
headers: {
"X-API-KEY": "23dq3dq3ddbb16a7956e6f7a",
"Content-Type": "application/json"
},
type: "PUT",
data: fromDatan,
dataType: "JSON",
success: function(fromData, status, jqXHR) {
alert(JSON.stringify(fromData));
},
error: function(jqXHR, status) {
alert(JSON.stringify(jqXHR));
}
});
So please let me know how to do this. Help me out
the code that runs every second should do:
fromDatan.values.push({
at: timestamp,
value: value
});
if (fromDatan.values.length >= 50) {
$.ajax( {
...
});
fromDatan.values = [];
};
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 8 years ago.
Using Flickr API, Javascript/JQuery, and AJAX, I have the follow code:
function getRequest(arguments)
{
var requestinfo;
$.ajax({
type: 'GET',
url: flickrurl + '&'+ arguments + '&jsoncallback=jsonCallback',
async: false,
jsonpCallback: 'jsonCallback',
dataType: 'jsonp',
success: function (json)
{
requestinfo = json;
//Parse attempt requestinfo = $.parseJSON(json);
//Old method return json;
}
});
return requestinfo;
}
When the response is received and I attempt to assign the json (the response) data to the variable, requestinfo, (or even if I do a straight 'return json' line) I get an error indicating the returned value is undefined. Upon fooling with the success function, I notice that any attempts to parse the response fails due to 'json' being read as [object Object] instead of the JSON response.
Is the response already in array format? If not, how can I get it to that point?
Here is an example JSON response (there are multiple types as different requests are needed to receive all needed information (Flickr API):
{
"collections": {
"collection": [
{
"id": "122388635-72157643471284884",
"title": "Test Gallery 2",
"description": "Test 2",
"iconlarge": "/images/collection_default_l.gif",
"iconsmall": "/images/collection_default_s.gif",
"set": [
{
"id": "72157643471292484",
"title": "Set 1",
"description": ""
}
]
},
{
"id": "122388635-72157643469075734",
"title": "Test Gallery 1",
"description": "Bing Photos",
"iconlarge": "http://farm3.staticflickr.com/2888/cols/72157643469075734_b9a37df67c_l.jpg",
"iconsmall": "http://farm3.staticflickr.com/2888/cols/72157643469075734_b9a37df67c_s.jpg",
"set": [
{
"id": "72157643469056814",
"title": "Test Gallery 1",
"description": "Bing Backgrounds"
}
]
}
]
},
"stat": "ok"
}
So how do I pass the received data to other functions without a disruption in the data?
Code should be much like below
success: function (collections)
{
$.each(collections,function(index,item){
var i=0;
$.each(item.conllection[i],function(index,item){
alert(item.id);
i++;
});
});