I can't get value from Json Array in ExtJS
Example for my code:
var array = [];
array.push({"id": data.id, "name": data.name});
I tried below codes but I couldn't get it. How should I do?
array[0].data.data.id;
array[0].data.id;
array[0].data.data.get('id');
I solved it with this code:
array.items[0].data.id;
Related
I have a JSON file like following:
[{"PROV_32_ID":1.0,"Pname":"Kabul","Time":9107,"value":24.0,"Children59m":0.0},
{"PROV_32_ID":1.0,"Pname":"Kabul","Time":9110,"value":92.0,"Children59m":8.0},
{"PROV_32_ID":1.0,"Pname":"Kabul","Time":9111,"value":78.0,"Children59m":4.0}]
I would like to create another JSON with only three columns like following from the above JSON object.
[{"Pname":"Kabul","Time":9107,"value":24.0},
{"Pname":"Kabul","Time":9110,"value":92.0},
{"Pname":"Kabul","Time":9111,"value":78.0}]
Anybody, please help.
You can use the for loop
UPDATED
var json = [{"PROV_32_ID":1.0,"Pname":"Kabul","Time":9107,"value":24.0,"Children59m":0.0},{"PROV_32_ID":1.0,"Pname":"Kabul","Time":9110,"value":92.0,"Children59m":8.0},{"PROV_32_ID":1.0,"Pname":"Kabul","Time":9111,"value":78.0,"Children59m":4.0}]
for(var i in json)
{
delete json[i]['PROV_32_ID'];
json[i]['value'] = json[i]['Children59m'];
delete json[i]['Children59m'];
}
console.log(json)
so i have this data which is outputted from stringify(doc)
{
"id": "8467fdae-c38c-4b6e-9492-807d7c9eb97e",
"message": "send to nodejs"
}
but im not sure how can i go ahead in getting a single value from it, e.g message. Any help would be appreciated as ive tried different methods and they seem to not be working
After you use stringify method the value you have is string.
So You can not get single property from it, except you convert it become object again and get property it. Like this:
object = {
"id": "8467fdae-c38c-4b6e-9492-807d7c9eb97e",
"message": "send to nodejs"
};
// JSON string is value which you get after use Stringify method
jsonString = JSON.stringify(object);
// Convert jsonString to object again and get message property
message = JSON.parse(jsonString).message
stringify() will convert json to string.
Before running stringify(), doc is already json and you can get message by doc.message.
after running stringify(), the result will be string, if you want to get json back, you can use JSON.parse()
I want to store the result from a call to a Domino Access Service (DAS) in a localStorage however when I try to convert the result object to a JSON string I get an error.
With DAS you get the result as an Array e.g.:
[
{
"#entryid":"1-CD90722966A36D758025725800726168",
"#noteid":"16B46",
Does anyone know how I get rid of the square brackets or convert the Array quickly to a JSON object?
Here is a snippet of my code:
var REST = "./myREST.xsp/notesView";
$.getJSON(REST,function(data){
if(localStorage){
localStorage.setItem('myCatalog',JSON.stringify(data));
}
});
Brackets are part of the JSON syntax. They indicate that this is an array of objects. And as you point to a view it is very likely that you would get more than one object back (one for each entry in the view).
So if you are only interested in the first element you could do this:
var REST = "./myREST.xsp/notesView";
$.getJSON(REST,function(data){
if(localStorage){
var firstRecord = data[0] || {};
localStorage.setItem('myCatalog',JSON.stringify(firstRecord));
}
});
Otherwise, you would need to define a loop to handle each of the objects :-)
/John
I am running into an error parsing a json string with JSON.parse(...)
var str:String= '[{"AA":"A1", "BB":"32"}, {"AA":"A2", "BB":"12"}, {"AA":"A3", "BB":"14"}]';
var propertySets:Object = JSON.parse(str);
I can tell that I am getting the syntax wrong with constructing the JSON string but I have tried quite a few things before giving up. Any help with how to deal with collections would be great.
The following simple case works for me
var str:String= '{"test":"line1"}';
var propertySets:Object = JSON.parse(str);
Thank you
The syntax in the JSON string is correct. The way it is formatted will return an Array instance from JSON.parse(). The following code works for me:
var str:String = '[{"AA":"A1", "BB":"32"}, {"AA":"A2", "BB":"12"}, {"AA":"A3", "BB":"14"}]';
var propertySets:Array = JSON.parse(str) as Array;
trace(propertySets[0].AA); // prints "A1"
trace(propertySets[0].BB); // prints "32"
Shot in the dark since I don't know actionscript, but try wrapping the array in an object
var str:String= '{"objectArray":[{"AA":"A1", "BB":"32"}, {"AA":"A2", "BB":"12"}, {"AA":"A3","BB":"14"}]}';
Essentially I'm trying to parse JSON and assigning the results to variables, one of which is "var = JSON.class;" with class being what's returned in the JSON. However, flash won't let me parse it because it's called class which it uses to create new classes. Is there any workaround or will I not be able to grab this node?
I'm not sure if this is what you're asking, but if you have a property in your JSON object named "class", you should be able to parse it like as shown below.
// assuming JSON object looks like this, and is stored in a var named 'jsonData'
var jsonData:Object = { "id": 0, "class": "MyClassName", "values": [1,2,3] }
// trying to parse like this won't work w/keywords like 'class':
var parsedValue : String = jsonData.class;
// parse it with this way, using the square brackets:
var parsedValue : String = jsonData["class"];