how can i get the first "id_miembro" for example of this:
{"actividades":[{"id_miembro":"V-005","id_dpto":"AC-04","id_actividad":"D-01","id_seccion":"S-03"},{"id_miembro":"V-005","id_dpto":"AC-02","id_actividad":"D-01","id_seccion":"S-01"}]}
I've tried with other ways but nothing, thanks.
Parsing a JSON string in javascript can be done with JSON.parse(). For your JSON you can do this to get the id_miembro of the first item in the list.
var json = '{"actividades":[{"id_miembro":"V-005","id_dpto":"AC-04","id_actividad":"D-01","id_seccion":"S-03"},{"id_miembro":"V-005","id_dpto":"AC-02","id_actividad":"D-01","id_seccion":"S-01"}]}';
var obj = JSON.parse(json);
var actividades = obj.actividades;
var first = actividades[0];
var id_miembro = first.id_miembro;
I broke it down into seperate variables so it's easier to follow which line does what.
If your data is alread a javascript object you can skip the first two lines and just do this
var obj = {"actividades":[{"id_miembro":"V-005","id_dpto":"AC-04","id_actividad":"D-01","id_seccion":"S-03"},{"id_miembro":"V-005","id_dpto":"AC-02","id_actividad":"D-01","id_seccion":"S-01"}]};
var actividades = obj.actividades;
var first = actividades[0];
var id_miembro = first.id_miembro;
Related
I been trying to JSONParse this rest api, but haven't had any luck. Would anyone be able to see why I keep getting either [object Object] or undefined error?
<div id="store_1_name"></div>
<div id="owner_1_name"></div>
<div id="friend_1_name"></div>
var raw_data_string = '{"2":[{"buy":[{"house":"loan","condo":{"USD":64311.9005450},"apartment":{}},{"house":"rent","condo":{"USD":2015.38570},"apartment":{}}]}';
var data = JSON.parse(raw_data_string);
for (var i in data) {
ParseUpdate(data[i].house, data[i].condo, data[i].apartment);
}
function ParseUpdate(house, condo, aparment) {
var house = data[i].house;
var condo = data[i].condo;
var apartment = data[i].buy.apartment;
}
document.getElementById("store_1_name").innerHTML = data[0].house;
document.getElementById("owner_1_name").innerHTML = data[0].condo;
document.getElementById("friend_1_name").innerHTML = data[0].apartment;
Thanks all!
You are doing so many things wrong here. 1st, the JSON string is invalid. 2nd, You're trying to access wrong elements inside the for loop. 3rd, data[i].buy.apartment doesn't exist if you're also accessing data[i].house in the same loop as they are inside the same object which is buy. 4th, you're trying to show JSON object in the innerHTML of a div where you should access the string value you want to show or stringify the whole object.
var raw_data_string = '{"2":[{"buy":[{"house":"loan","condo":{"USD":64311.9005450},"apartment":{}},{"house":"rent","condo":{"USD":2015.38570},"apartment":{}}]}]}';
var data = JSON.parse(raw_data_string)[2][0]['buy'];
for (var i in data) {
ParseUpdate(data[i].house, data[i].condo, data[i].apartment);
}
function ParseUpdate(house, condo, aparment) {
var house = data[i].house;
var condo = data[i].condo;
var apartment = data[i].apartment;
}
document.getElementById("store_1_name").innerHTML = data[0].house;
document.getElementById("owner_1_name").innerHTML = JSON.stringify(data[0].condo);
document.getElementById("friend_1_name").innerHTML = JSON.stringify(data[0].apartment);
<div id="store_1_name"></div>
<div id="owner_1_name"></div>
<div id="friend_1_name"></div>
Your json string invalid
Check your data struct and correct the loop again (for)
apartment is an object but you show it same string
document.getElementById("friend_1_name").innerHTML = data[0].apartment; will not work
var raw_data_string =
'{"2":[{"buy":[{"house":"loan","condo":{"USD":64311.9005450},"apartment":{}},{"house":"rent","condo":{"USD":2015.38570},"apartment":{}}]}]}';
var data = JSON.parse(raw_data_string);
console.log(data);
There were some missing closing brackets in the raw_data_string. I would suggest JSON Formatter & Validator.
I have an array containing 3 elements
var a = [];
a["username"]=$scope.username;
a["phoneNo"]=$scope.phoneNo;
a["altPhoneNo"]=$scope.altPhoneNo;
Now, I want to send this data to server in JSON format. Therefore, I used
var aa = JSON.stringify(a);
console.log("aa = "+aa);
But the console displays empty array
aa = []
How can I convert this array into JSON?
That's not the correct way to add elements to an array, you're adding properties instead.
If you did console.log(a.username); you'd see your $scope.username value.
You could either do
var a = [];
a.push({"username": $scope.username});
a.push({"phoneNo": $scope.phoneNo});
a.push({"altPhoneNo": $scope.altPhoneNo});
But it looks more like what you're trying to do is
var a = {};
a["username"] = $scope.username;
a["phoneNo"] = $scope.phoneNo;
a["altPhoneNo"] = $scope.altPhoneNo;
That is, you want your a to be an object if you're going to add properties to it.
And that would be better written as
var a = {};
a.username = $scope.username;
a.phoneNo = $scope.phoneNo;
a.altPhoneNo = $scope.altPhoneNo;
I have the following code javascript code
var tasksPlayed = [];
tasksPlayed[90] = true;
var json = JSON.stringify(tasksPlayed);
var output = JSON.parse(json);
this gives an error
SyntaxError: JSON.parse
Im not sure why, if I do a .toSource() on the objects I get
keyValue:"[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,true]"})
Any ideas where I might be going wrong
I don't get this error when I run the code, but the thing with the null values is that tasksPlayed is an array, so when you set tasksPlayed[90] = true;, then it automatically fills all positions from 0 to 89 with null. Try using a plain object instead of an array:
var tasksPlayed = {};
tasksPlayed[90] = true;
var json = JSON.stringify(tasksPlayed);
var output = JSON.parse(json);
Cannot grab the follower count from the following URL. Shows up as blank. Am I grabbing the right parameters?
https://developer.foursquare.com/docs/explore#req=pages/search%3Fname%3DNintendo
function foursquareFriends(url) {
var responses = UrlFetchApp.fetch(url);
var json = responses.getContentText();
var data = JSON.parse(json);
var likes = data.response.results.followers.count;
return likes;
}
You should do this (using this)
data.response.results[0].followers.count;
(And of course if you have more than 1 value - you should iterate).
I have an array:
var pages = new Array();
I want to push my pages data to this array like this:
$('li.page').each(function () {
var datatype = $(this).attr('data-type');
var info = $(this).attr('data-info');
pages_order.push({datatype:info});
});
but this code doesn't replace datatype as variable, just puts datatype string as a key.
How do I make it place there actual string value as a key name?
I finally saw what you were trying to do:
var pages = new Array();
$('li.page').each(function () {
var datatype = $(this).attr('data-type');
var info = $(this).attr('data-info');
var temp = {};
temp[datatype] = info;
pages_order.push(temp);
});
$('li.page').each(function () {
//get type and info, then setup an object to push onto the array
var datatype = $(this).attr('data-type'),
info = $(this).attr('data-info'),
obj = {};
//now set the index and the value for the object
obj[datatype] = info;
pages_order.push(obj);
});
Notice that you can put a comma between variable declarations rather than reusing the var keyword.
It looks like you just want to store two pieces of information for each page. You can do that by pushing an array instead of an object:
pages_order.push([datatype, info]);
You have to use datatype in a context where it will be evaluated.
Like so.
var pages = [];
$('li.page').each(function () {
var datatype = $(this).attr('data-type'),
info = $(this).attr('data-info'),
record = {};
record[datatype] = info;
pages_order.push(record);
});
You only need one var it can be followed by multiple assignments that are separated by ,.
No need to use new Array just use the array literal []
You may add below single line to push value with key:
pages_order.yourkey = value;