i have json response like this :
{
"gasal": [
{
"prodi": "Teknik Industri",
"jumlah": "3.5000"
},
{
"prodi": "Teknik Informatika",
"jumlah": "6.0000"
}
],
"genap": [
{
"prodi": "Teknik Informatika",
"jumlah": "2.0000"
}
],
"prodi": [
{
"nama_prod": "Teknik Informatika"
},
{
"nama_prod": "Teknik Industri"
}
]}
i want to show the response to bar chart (i use chart js), but the problem is i dont know how to call the json response in javascript.
this is my javascript code :
var url = "{{url('test')}}";
var Prodi = new Array();
var gasal = new Array();
var genap = new Array();
$.get(url, function(response){
response.forEach(function(data){
Gasal.push(data.genap.jumlah);
Genap.push(data.gasal.jumlah);
Prodi.push(data.prodi.nama_prod);
});
})
i think the problem is in this code
Gasal.push(data.genap.jumlah);
Genap.push(data.gasal.jumlah);
Prodi.push(data.prodi.nama_prod);
what is the right code ?
thank you.
Gasal.push(data.genap[0]) would be the whole object
Gasal.push(data.genap[0].jumlah) would be the property of the object
Because you have created an array with an object inside and then you need to access the properties of the object.
Related
I have an input json as:
{
"setInfo":{
"userset1":{
"details1":[
"somedata1",
"somedata2"
],
"details2":[
"somedata3",
"somedata4"
]
},
"userset2":{
"details3":[
"somedata5",
"somedata6"
],
"details4":[
"somedata7",
"somedata8"
]
}
}
}
I want to convert this into below output json:
{
"setInfo":{
"userset1":{
"details1":{
"options":[
"op1",
"op2",
"op3"
]
},
"details2":{
"options":[
"op1",
"op3"
]
}
},
"userset2":{
"details3":{
"options":[
"op1"
]
},
"details4":{
"options":[
"op4"
]
}
}
}
}
In the above input json somedata1, somedata2...somedata8 are variables that I need to pass to a service endpoint that would return me corresponding options for details1.....details4 which are op1,op2 etc
So I want to loop through the above input json and update the above with values returned from my service.
So I tried with the below code.
//my inital service call to get data
myservice.getData().then(function(result){
// Below is console output from result.setInfo
// setInfo:
// userset1:
// details1: (2) ["somedata1", "somedata2"]
// details2: (2) ["somedata3", "somedata4"].
// userset2:
// details3: (2) ["somedata5", "somedata6"]
// details4: (2) ["somedata7", "somedata8"]
//Now I tried looping through the json and update but not getting through as I feel something is incorrect below
_.each(result.setInfo, function (set, name) {
$scope.sets[name] = {};
_.each(set, function (data1, name1) {
//This is the service which gets corresponding options op1,op2 etc according to the params that were passed
myservice.getArrayData({ data: data1}).function((res){
//this is where I feel something is not correct. .pluck gets the correponding data fine but its the assignment which is not correct.
$scope.sets[name].setInfo[name].options = _.pluck(res, 'name');
});
});
});
});
With the above code I get the below json which is not what I expect,
{
"setInfo":{
"userset1":{
"options":[
"op1",
"op2"
]
},
"userset2":{
"options":[
"op1",
"op2"
]
}
}
}
So see how it removed some tags above.
Could anyone point me to whats missing.
Sorry for long post but wanted to cover as much as I can.
--Updated--
So I updated to use this code
$scope.setInfo[name][name1] = _.pluck(jobs, 'name');
And with this I made further progress that now my output json became as:
{
"setInfo":{
"userset1":{
"details1":[
"op1",
"op2",
"op3"
],
"details2":[
"op1",
"op3"
]
},
"userset2":{
"details3":[
"op1"
],
"details4":[
"op4"
]
}
}
}
So the only thing missing here is how to add the options tag.
I tried with this
$scope.setInfo[name][name1].options = _.pluck(jobs, 'name');
But this gives me error:
Cannot set property 'options' of undefined
I am writing to a json file in casperjs and am trying to add new objects to it.
json file looks like
{ "visited": [
{
"id": "258b5ee8-9538-4480-8109-58afe741dc2f",
"url": "https://................"
},
{
"id": "5304de97-a970-48f2-9d3b-a750bad5416c",
"url": "https://.............."
},
{
"id": "0fc7a072-7e94-46d6-b38c-9c7aedbdaded",
"url": "https://................."
}]}
The code to add to the array is
var data;
if (fs.isFile(FILENAME)) {
data = fs.read(FILENAME);
} else {
data = JSON.stringify({ 'visited': [] });
}
var json = JSON.parse(data);
json.visited.push(visiteddata);
data = JSON.stringify(json, null, '\n');
fs.write(FILENAME, data, "a");
This is starting off by adding an new { "visited" : [ ] } array with first couple of objects, below the existing { "visited" : [ ] } array and subsequently the script breaks because the json array is no longer valid json.
Can anybody point me in the right direction. Thank you in advance.
You have a JSON file containing some data.
You:
Read that data
Modify that data
Append the modified version of that data to the original file
This means the file now has the original data and then, immediately after it, a near identical copy with a little bit added.
You don't need the original. You only need the new version.
You need to write to the file instead of appending to it.
Change the 'a' flag to 'w'.
Trying to access the value field of this JSON file using JSON.parse() in Meteor, but I cannot get it to return anything. I suspect there is an error in my syntax in selecting the data from the imported JS object.
{"status":"success","data":{"subjects":[{"value":"ABC","descr":"Descriptions"}]},"message":null,"meta":{"copyright":"Copyright","referenceDttm":"Date"}}
I'm trying to store it into an array, subjectArray. This is the code I'm using:
var subjectArray = new Array();
subjectFile = HTTP.get("https://classes.cornell.edu/api/2.0/config/subjects.json?roster=FA15");
subjectJSON = JSON.parse(subjectFile);
for (int i=0; i<subjectJSON.length; i++) {
subjectArray.push(subjectJSON[i].value)
}
Pretty printed this is:
{
"data": {
"subjects": [
{
"descr": "Descriptions",
"value": "ABC"
}
]
},
"message": null,
"meta": {
"copyright": "Copyright",
"referenceDttm": "Date"
},
"status": "success"
}
Responses from HTTP calls can take a while to come back so you should read your replies in an async way. You should move all of your code related to the "get" inside a callback function. If you want to find out more about HTTP and callbacks in Meteor make sure you check the docs
If you know what "value" is in your for loop ('cause I don't), then this is your answer:
HTTP.get("https://classes.cornell.edu/api/2.0/config/subjects.json?roster=FA15", function (err, res) {
if(!!err) return false;
subjectJSON = JSON.parse(res);
for (var i = 0; i < subjectJSON.length; i++) {
subjectArray.push(subjectJSON[i].value);
}
return true;
});
Also, there is no int in JavaScript.
Have got a json file exported from mysql. One particular line is not a well represented json object, i'm trying to convert this to a proper array of object.
var data = "{"54":
{"ID":"54",
"QTY":"1",
"NAME":"Large",
"TOTAL":1.86
},
"TOTAL":10.54,
"313":
{"ID":"313",
"QTY":2,
"NAME":"Quater Pounder",
"TOTAL":8.68
}
}"
//and wants to make it:
var data = [
{"ID" : "54",
"QTY" : "1",
"NAME": "Quarter Pounder",
"TOTAL": 8.68
},
{"ID":"313",
"QTY":2,
"NAME": "Quater Pounder",
"TOTAL":8.68
}
]
I was able to fix this by using angular.forEach(response, function(item){}), I then created a childArray, which I pushed the result of the above into.
Please see code:
angular.forEach( $scope.response, function (item) {
item.childrenList = [];
angular.forEach( JSON.parse( item.details ), function (value, id) {
item.childrenList.push( value );
})
});
I am trying to put my ajax call returned data and other codes in one function.
This is for custom fckeditor plugin.
I have something like
function customTag(editor){
var id = editor.config.id;
var instance = this;
//my custom ajax wrapper…….
//the 'dbData' is holding the returned data from ajax.
ajax.onFinished = function(dbData){ console.log(dbData)};
//I want to return this object and use my ajax returned data
return {
title:'Link',
minWidth : 200,
minHeight : 200,
buttons : [CKEDITOR.dialog.okButton, CKEDITOR.dialog.cancelButton],
contents: [
{
id:'tab',
label: 'test here',
elements: [
{
type:'select',
id:'select box',
//I want to use the returned data below
items: [[dbData[0],0],[dbData[1],0] ]
}
]
}
]
}
}
CKEDITOR.dialog.add('customTag', function(editor){
return customTag(editor);
});
How would I be able to solve this. Thanks a lot!
Perform CKEDITOR.dialog.add() inside ajax.onFinished. In there, create the return object and use it directly for CKEditor. Either that, or use synchronous operations. Something like this:
ajax.onFinished = function(dbData){
var o = {
title:'Link',
minWidth : 200,
minHeight : 200,
buttons : [CKEDITOR.dialog.okButton, CKEDITOR.dialog.cancelButton],
contents: [
{
id:'tab',
label: 'test here',
elements: [
{
type:'select',
id:'select box',
items: [[dbData[0],0],[dbData[1],0] ] // Use dbData
}
]
}
]
};
CKEDITOR.dialog.add('customTag', function(editor){
return o;
});
};
If CKE has issues with calling dialog.add after it's been initialized, initialize it inside ajax.onFinished too.