Access Value from JSON.parse - javascript

I'm having issues accessing values from json using json.parse. This is my JSON:
[
{
"store":[
{
"name":"Grand Theft Auto V"
},
{
"skuid":"UP1004-CUSA00419_00-GTAVDIGITALDOWNL-U001"
},
{
"releasedate":"2014-11-18T00:00:00Z"
} //...
I'm trying to get the name from the store array.
var cif = JSON.parse(response);
// Tried the following:
alert(cif["store"][0].name);
alert(cif.store[0].name);
alert(cif[0].store.name);
alert(cif["store"].name);
if(cif.store[0].name) $("#cif-title").html(cif.store[0].name);
How do I access this value?

This should work.
alert(cif[0]["store"][0]["name"]);
or
alert(cif[0].store[0].name);

Try console.log(cif[0].store[0].name)

Related

Send JSON with unusual keys

I've object in js with keys-selectors:
{
"[data-index="0"]": {
// something
},
"> .class > .one": {
key: "very bad+ \"\' string"
}
}
How send this object via ajax without any changes in the keys and with correct values?
By correct values I mean that "very bad+ \"\' string" should be total escaped, but save all chars and signs to store in Database.
PS. I know that i can send objects via AJAX
JSON.stringify will transform your object into a JSON string. It can handle special characeters, don't worry.
var json = JSON.stringify(myObject);
When you modify your object a bit
var obj = {
"[data-index='0']": {
// something
},
"> .class > .one": {
key: "very bad+ \"\' string"
}
}
then a JSON.stringify(obj) works. The second pair of " after data-index= provokes an error.
-- edited syntax

JavaScript, JSON, referencing by name

How do you reference a JSON object in JavaScript?
I have a JSON response from a Rest web service and trying to reference the contents of the response which I have parsed to JSON by way JSON.Parse(response)
Sample JSON:
{
"HotelListResponse":{
"customerSessionId":"",
"numberOfRoomsRequested":1,
"moreResultsAvailable":true,
"cacheKey":"",
"cacheLocation":"",
"cachedSupplierResponse":{
"#supplierCacheTolerance":"NOT_SUPPORTED",
"#cachedTime":"0",
"#supplierRequestNum":"101",
"#supplierResponseNum":"",
"#supplierResponseTime":"",
"#candidatePreptime":"14",
"#otherOverheadTime":"",
"#tpidUsed":"",
"#matchedCurrency":"true",
"#matchedLocale":"true"
},
"HotelList":{
"#size":"20",
"#activePropertyCount":"101",
"HotelSummary":[
{
"name":"name1"
},
{
"name":"name2"
}
]
}
}
}
How can I, for example reference the customerSessionId? And the second HotelSummary name?
For customerSessionId I have tried jsonObject.customerSessionId which returns undefined. For the second hotel summary name I have tried jsobObject.HotelList.HotelSummary[1].name which is undefined too.
Given the JSON string above parsed and assigned to a variable as such:
var response = JSON.Parse(jsonString);
you should be able to access it like this:
var customerSessionId = response.HotelListResponse.customerSessionId;
Here's the working solution fiddle
As you can see, you need to reference HotelListResponse,
so if your var result holds your json object, then you can fetch the values by using
var first = result.HotelListResponse.customerSessionId
var second = result.HotelListResponse.HotelList.HotelSummary[1].name

adding variable values in json file using push function

I have a json file and two variables, i want to store the variables values in the json file using push function in json. my code is
var x=xmen;
var z=xmen website
var jsonObj = {
"items":
[
{
"title":"some title",
"url":"some url"
}
]
};
I want my resulting json file to be
var jsonObj = {
"items":
[
{
"title":"some title",
"url":"some url"
}
{
"title":"xmen",
"url":"xmen website"
}
]
};
I dont want to use arrays just while pushing the value using
jsonObj.items.push
i want to call the variable and assign like example
jsonObj.items.push({"title":+x+,"url":+url+}); //just to explain, its not the original function.
I don't think push is your problem. It's your object syntax that's messed up.
Instead of
{"title":+x+,"url":+url+}
Try this instead:
// Assuming that "x" and "url" are valid variables
{"title":x,"url":url}
Here's a quick demo of everything working: http://jsbin.com/zicofoye/1/edit
jsonObj.items.push({
"title":"avengers",
"url":"avengers website"
});

json web service Get value from dynamic parameter's name

please i have this output from my json web service:
{"format":"json",
"success":true,
"errors":[],
"result":
{"**206**":
{"player_name":"stagiaire",
"M":{
"6480":{"score":0,"answer":"cdgvbdbgd","category_name":"cdgvbdbgd"},
"6481":{"score":0,"answer":"cdgvbdbgd","category_name":"cdgvbdbgd"},
"6482":{"score":0,"answer":"cdgvbdbgd","category_name":"cdgvbdbgd"},
"6483":{"score":0,"answer":"cdgvbdbgd","category_name":"cdgvbdbgd"},
},
"O":{
..... },
}
}
}
What i want is to extract the value (which is now 206 but it can be another number).
I am asking for your help to achieve this goal.
Any help will be appreciated.
In case the resulting object contains only a single property with this number as its name, i.e.
result = {
206 : {
...
}
// no other properties here
}
then you can use something like:
var num = Object.keys(result).shift(); // "206"
Check the browser compatibility for Object.keys() method at MDN and use shim if needed.
If the object you look for always has a player name as value, try
for (var key in obj.result) if obj.result[key]["player_name") alert(key)
Or use jQuery
$.each(data.result,function(key,val) {
alert(key);
});

Accessing JSON elements from javascript

$.getJSON('http://23.21.128.153:3000/api/v1/holidays', function(data){
alert("this: " + data.holiday[0].name);
});
I'm trying to access the "name" attribute of the first element of my JSON response but without success, can anyone tell me what I'm doing wrong.
Try this:
data[0].holiday.name
The data looks like this:
[
{
"holiday":{
"id":1,
"date":"2012-05-01",
"name":"Dia del trabajo",
"description":"",
"country_id":1,
"moved_date":"2012-04-30"
}
},
{
"holiday":{...}
},
...]
So, you need to select the first element from the main array (data[0]), then get its holiday property (data[0].holiday), and then get its name property.

Categories

Resources