how to access the information1 data - javascript

Am trying to access the data from the following JSON . Am Getting response after hitting the server and i need to access the data present in the information1 field.
{
"Test1":[
{
"id1":0,
"Test2":[
{
"Information1":"info1",
"name":"Testing",
"defnitions":[
{
"displayname":"displayame"
},
{
"displayname2":"displayame2"
}
],
"information2":"info2"
}
],
"information3":"info3",
"information4":"info4"
}
]
}

Cause you are using square brackets, it means you have an array. Consequently, to access to your neccessary object you should use:
var data={
"Test1":[
{
"id1":0,
"Test2":[
{
"Information1":"info1",
"name":"Testing",
"defnitions":[
{
"displayname":"displayame"
},
{
"displayname2":"displayame2"
}
],
"information2":"info2"
}
],
"information3":"info3",
"information4":"info4"
}
]
};
var yourObject=data.Test1[0].Test2[0];
To parse JSON response just use stringify method:
var theWholeObject=JSON.stringify(data);

Related

How to get customised json in angular

I want to create dynamic json from response coming from api?? How can i acheive this?? I want to have json as shown below. But i am getting invalid json. What am i doing wrong??
{
"data":[
{
"dataOverallCount":response.dataOverallCount,
"dataTotalCount":response.dataTodaysCount
}
],
"item":[
{
"itemOverallCount":response.itemOverallCount,
"itemTotalCount":response.itemTotalCount
}
]
}
here response is an object as
{dataOverallCount: 2, dataTodaysCount:0, itemOverallCount:6, itemTotalCount:3}

Update existing json object

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

jQuery each loop for json data by index

I have some json data that the id number will change, so instead of referencing it by ID number I would like to reference it by array index.
I am looking to alert the source which would be the image URL https://upload.wikimedia.org/wikipedia/commons/thumb/8/89/Chuck_Schumer_official_photo.jpg/240px-Chuck_Schumer_official_photo.jpg
This is a sample of the json data:
{
"batchcomplete":"",
"query":{
"normalized":[
{
"from":"Chuck_Schumer",
"to":"Chuck Schumer"
}
],
"pages":{
"326708":{
"pageid":326708,
"ns":0,
"title":"Chuck Schumer",
"thumbnail":{
"source":"https://upload.wikimedia.org/wikipedia/commons/thumb/8/89/Chuck_Schumer_official_photo.jpg/240px-Chuck_Schumer_official_photo.jpg",
"width":240,
"height":300
},
"pageimage":"Chuck_Schumer_official_photo.jpg"
}
}
}
}
This is my JavaScript attempt:
function processWikiData(wikiData){
$(wikiData.query).each(function(index, query_item) {
$(query_item.pages).each(function(index, page_item) {
// I was hoping to get the array's first index here but clearly this is wrong
$(page_item.[0]).each(function(index, wiki_id) {
$(wiki_id.thumbnail).each(function(index, thumbnail_item) {
alert(thumbnail_item.source);
});
});
});
});
}

Javascript (Appcelerator Titanium) Multidimensional Array parse doesn't work

I have an problem with the parsing of an multidimensional array, i get this from facebook as an answer:
In e.result i have this:
{
"data":[
{
"name":"Linda Kase",
"id":"1393034660999695"
},
{
"name":"Dick Typ",
"id":"1376046576034204"
},
{
"name":"a a",
"id":"1388801108093951"
},
{
"name":"b b",
"id":"1382328532076389"
}
],
"paging": {
"next":"https:\/\/graph.facebook.com\/v2.2\/1378163649155341\/friends?format=json&access_token=XXXXXXXXXXXXXXXXXXX"
},
"summary":{
"total_count":8
}
}
and this is my code to parse it:
Ti.App.fb.requestWithGraphPath('me/friends',false, 'GET', function(e){
if(e.success){
var result = e.result;
alert(result.data[0].name);
}
});
I always recieve this:
Uncaught TypeError: Cannot read property '0' of undefined
Anyone an idea why it isn't working ? I've also tried to JSON.stringify and JSON.parse the e.result
Thank you!
You have to use:
var result = JSON.parse(e.result);
alert(result.data[0].name);
because you get it as a string, not as an array

Cant access data from json

I'm having trouble with JSON. I made this in PHP and I'm sending it to my JavaScript, but I can't get the values.
[
{
"book":[
{
"dir":"extract\/pg1065.epub"
},
{
"dir":"extract\/pg1065.epub\/1065\/0.css"
},
{
"dir":"extract\/pg1065.epub\/1065\/1.css"
},
}
{
"book":[
{
"dir":"extract\/pg6130-images.epub"
},
{
"dir":"extract\/pg6130-images.epub\/6130\/0.css"
},
}
]
I'm trying to access it with
var obj = JSON.parse(result);
alert(obj.book[0].dir[1]);
Anyone have any ideas?
First you need to validate your json, i have validate your json it gives error.
In your json dIr is id.
You have defined 3 dir id for same object this may be error.
EDIT: I missed it but first comment explains your missing your closing square brackets for the book arrays. Add that in and your good to go. Validate the JSON first.
You don't need to do JSON.parse you can simply do
var data = <?php echo "Your generated JSON code"; ?>;
Worth a note you can create your data structure in PHP and then simply use json_encode, then you can be sure it will be valid JSON
var data = <?php echo json_encode($yourData); ?>;
You have output an array so to get the first object you will do something like
var firstObj = data[0];
To get the first dir of the first book
var firstDir = data[0]["book"][0]["dir"];
[
{
"book": [
{
"dir": "extract/pg6130-images.epub"
},
{
"dir": "extract/pg6130-images.epub/6130/0.css"
}
]
},
{
"book2": [
{
"dir": "extract/pg6130-images.epub"
},
{
"dir": "extract/pg6130-images.epub/6130/0.css"
}
]
}
]
Your JSON was not valid i used: http://jsonlint.com/ to sort it!
Now you should be able to acess the data fine.
The code shown in the question is not a valid JSON. There are missing closing square brackets for each of the book arrays and (thanks to #punund) a missing comma between array members. The correct JSON would be this:
[
{
"book":[
{
"dir":"extract\/pg1065.epub"
},
{
"dir":"extract\/pg1065.epub\/1065\/0.css"
},
{
"dir":"extract\/pg1065.epub\/1065\/1.css"
}
]
},
{
"book":[
{
"dir":"extract\/pg6130-images.epub"
},
{
"dir":"extract\/pg6130-images.epub\/6130\/0.css"
}
]
}
]
You should not normally be printing JSON directly, but instead creating a JSON object in PHP and then using json_encode function. The following PHP will produce valid JSON for your scenario:
<?php
$result = array(
(object)array("book" => array((object)array("dir" => "extract/pg1065.epub"),
(object)array("dir" => "extract/pg1065.epub/1065/0.css"),
(object)array("dir" => "extract/pg1065.epub/1065/1.css"))),
(object)array("book" => array((object)array("dir" => "extract/pg6130-images.epub"),
(object)array("dir" => "extract/pg6130-images.epub/6130/0.css")))
);
echo json_encode($result);
?>

Categories