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
Related
I am trying the below code to store the token for "TokenType": "UserAccount"into a variable but it doesn't seem to work.Please help.
My Code:
if (responseCode.code === 200) {
try {
var tokenValue = JSON.parse(responseBody);
} catch (e) {
if (e) {
console.log(e);
}
}
}
postman.setGlobalVariable("Acc_Token", tokenValue[0].value);
Array consisting of token values:
"value": [
{
"Expires": "2017-09-05T05:07:11.0778675Z",
"IsRefreshable": false,
"Token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzUxMiJ9.eyJhdWQiOiJodHRwOi8vcWEubWFzc2l2ZWF4aXMuY29tL0lTTC9BcGkvVjEvRGF0YXNlcnZpY2UiLCJzdWIiOiJTZXR0aW5ncyIsImV4cCI6MTUwNDU4ODAzMSwidXNlckFjY291bnRJZCI6Ijg5MjRkMzFkLWFhYjktNDkzYy05N2NlLWRkMTc5NzQyNGE3ZSIsInVzZXJQcm9maWxlSWQiOiIyY2YyZWEyNC01NTRmLTQzYzktYjA1NS1hMjM2NGVjYWY0ZjkiLCJlbWFpbCI6ImFwYXJuYTExNEBtYXNzaXZlLmNvbS5hdSIsImRldmljZSI6IndlYl9icm93c2VyIiwidm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ndlYl9icm93c2VyIn0.JoaEijd6baSsHiVofeP3jUowJxp91SDcFb4zANj_ZnR0gHpp2yqCBQD4zh22maB7Yp4m5TD8jkfOuj9id2TD0N6pCa8Y9fXNQGAnZy963XrwnRz14AEfg954AGeAHSw6_qQGxh-xlc63rlsn62gnmZ0yPZZDHHbjYngJKsr8Kv_5qBNjZ9TX2y8H0PAm_STkw5PXqaCjtfMFPVyGFvDpVzx2wj1mMwnAS7V6Ftnr1-flaIjRmGXv0XQiND-uzxEZa8wjfLOEbayNS1IgbBRuiixaOKUvul14FwxasdojnZ5UOB5R_I4OUzp6WjWTax2rjbvIXxdlj0iyLeL7WD6egQ",
"TokenType": "UserProfile"
}
]
Error:
There was an error in evaluating the test script: TypeError: Cannot read property 'value' of undefined
Your value property is not in the first item. It is in the object itself
Accessing via tokenValue.value[0] will give you that object which has "TokenType": "UserAccount".
And also move your postman.setGlobalVariable into the if statement.
I cannot get the items of an JSON array within an JSON object. Here's what my JSON looks like:
[
{
"category":9,
"channels":[
{
"id":5,
"title":"MYTITLE",
"active":true,
"recent":true,
"image":"/arts/736c1ad4-2dbe-40a6-859d-8dba89c26ec2.jpg",
"recent_tracks":{
"vip":"https://api.xyzsite.com/recent_tracks/vip-3.json",
"free":"https://api.xyzsite.com/recent_tracks/free-3.json"
},
"additional_vip_channels":[
{
"channel_name":"vip-3a",
"recent_tracks_uri":"https://api.xyzsite.com/recent_tracks/vip-3a.json",
"streams":{
"320":"http://streams.xyzsite.com/api/914/320/stream",
"64":"http://streams.xyzsite.com/api/914/64/stream",
"192":"http://streams.xyzsite.com/api/914/192/stream"
}
}
],
"streams":{
"free":"http://streams.xyzsite.com/api/31/56/stream",
"free_56":"http://streams.xyzsite.com/api/31/56/stream",
"free_128":"http://streams.xyzsite.com/api/31/128/stream",
"320":"http://streams.xyzsite.com/api/33/320/stream",
"64":"http://streams.xyzsite.com/api/33/64/stream",
"192":"http://streams.xyzsite.com/api/33/192/stream"
}
},
I use the following code to get the values:
$.getJSON('https://api.xyzsite.com/channels.json', function(response) {
$.each(response, function (index, value) {
var catId = value.category;
$.each(value.channels, function (index, value) {
XYZApp.channels.push({
categoryId: catId,
id: value.id,
name: value.title,
image: value.image,
recent_tracks: {
vip: value.recent_tracks.vip,
free: value.recent_tracks.free
},
streams: {
free_128: value.streams.free_128,
member_320: value.streams["320"],
member_64: value.streams["64"],
member_192: value.streams["192"]
}
});
console.log(value.additional_vip_channels);
});
});
}).
then ...
I can get the values, but I cannot read the additional_vip_channels array within the .each jQuery function. I already tried:
value.additional_vip_channels.channel_name
value.additional_vip_channels[0].channel_name
value.additional_vip_channels["0"].channel_name
but none of them work.
And the log output is also here:
How can I get the channel_name and other data in additional_vip_channels?
I believe that you would want:
channels[0]['additional_vip_channels'][0]['channel_name']
Equally, this should work: (just a different way of selecting)
channels[0].additional_vip_channels[0].channel_name
Hope that helps and works :)
I have the following JSON.
{
"lang": [
{
"SECTION_NAME": {
"english": "My title"
},
"SECTION_NAME_2": {
"english": "My title"
}
}
]
}
And I'm looking to print the value like this:
$.getJSON('json/lang.json', function(data) {
var text = data['lang']['SECTION_NAME'];
$('#title').html(text.english);
});
But I have the following error:
TypeError: undefined is not an object (evaluating 'text.english')
Any help please.
Thanks.
You have to access it via index as lang is an array of object
like this
console.log(data['lang'][0]['SECTION_NAME'])
JSFIDDLE
The value of lang is an array which contains an object.
You are ignoring the array and trying to access the objects as if it was the value of lang directly.
I'm trying to update a JSON file with the value of a textarea using jquery push. I'm receiving the following error: " JavaScript runtime error: Unable to get property 'push' of undefined or null reference"
My jquery:
function submittedMsg(ctx) {
var id = $('.msg-input form').attr('id');
var newMsg = $('.msg-input textarea').val();
var url = "/ajax.aspx?vtl=ajax-conversation-json&cv=" + id;
$.getJSON(url, function (messageString, message) {
var message = [];
message.push({
msgcontent: newMsg,
sendname: sendRname,
mbrhref: mbrUrl,
datetime: ""
});
});
}
My JSON:
{
"messageString" :
[
{ "subject": "hello",
"msgstring": "5",
"unread": "1",
"datetime": "Oct 1 2013 9:59PM",
"orderid": "17",
"recipient": [
{
"mbrname": "Jane Doe",
"mbrhref": "/profile.aspx?mem=1227"
},
{
"mbrname": "John Smith",
"mbrhref": "/profile.aspx?mem=1337"
}
],
"message": [
{
"datetime":"2013-10-01T21:59:33.063",
"sendname":"Jane Doe",
"mbrhref":"/profile.aspx?mem=1227",
"msgcontent": "<p>Hi. I would like to talk with you about Dwarf Beryl Beauty</p>"
},
{
"datetime":"2013-11-26T16:29:17.037",
"sendname":"John Smith",
"mbrhref":"/profile.aspx?mem=1337",
"msgcontent": "Tough luck."
}
]
}
]
}
I don't necessarily need to use push to update the JSON file if there is a better way, I'm open to suggestions. I've verified my URL path is correct. Am I just missing something obvious? I'm new to JSON and only have passable jquery skills. Help!
Thanks in advance for any direction.
Try to use:
data.message.push
instead of:
data.messageString.message.push
Ah I see the issue, you have a local var and parameter of the same name message:
$.getJSON(url, function (messageString, message) { //here is param message
var message = []; //here is a local var parameter
message.push({ //this is probably referencing the parameter which is not an array or object that supports .push
Instead:
$.getJSON(url, function (data) { //I renamed the param to be more consistent with documentation, although it doesn't really matter, just will generate confusion
data.messageString.push({ //modify the json we were passed in the data param
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);
?>