I am reading data from a data base: I have the values I want
for (var index in entities) {
console.log(entities[index].PartitionKey);
console.log(entities[index].RowKey);
console.log(entities[index].Details);
console.log(entities[index].Date);
}
The above will print all the data I want. Now I want to convert this as an Json Object. How can I do that .
I have used and aware that I can use JSON.stringify but when I am trying this here in this context it is giving Error.
I tried keeping in the forloop:
jasonarray[index1].PartitionKey = entities[index].PartitionKey;
jasonarray[index1].RowKey = entities[index].RowKey;
and
JSON.stringify({ key: jasonarray[index1].PartitionKey, RowKey: jasonarray[index1].RowKey )
But it is giving as undefined when it comes to executing this function.
All I am looking is var x:
where x is a
[
{partionkey: "val",key: "val"}
{partionkey: "val",key1: "val"}
{partionkey: "val",key2: "val"}
]
Swift
13.2k5 gold badges58 silver badges80 bronze badges
asked Nov 24, 2012 at 20:38
The Learner
3,94714 gold badges42 silver badges52 bronze badges
1 Answer 1
Javascript: working sample http://fiddle.jshell.net/xrB8J/
var entities = [
{PartitionKey: 'a', RowKey: 5, Details:'details 1', Date:'01.01.2012' },
{PartitionKey: 'b', RowKey: 7, Details:'details 2', Date:'02.01.2012' },
{PartitionKey: 'c', RowKey: 3, Details:'details 3', Date:'03.01.2012' }
];
var a = new Array();
for(i = 0; i < 3; i++)
a.push({
PartitionKey: entities[i].PartitionKey,
RowKey: entities[i].RowKey
});
alert(JSON.stringify(a));
or c#:
string json = (new JavascriptSerializer()).Serialize(entities.Select(x => new
{
x.PartitionKey,
x.RowKey
}
));
answered Nov 24, 2012 at 20:58
Boris Gappov
2,49318 silver badges23 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-js
entitieslook like in its original state? If it is an array, you should not be usingfor in, whose purpose is iterating object properties.console.log(JSON.stringify(entities))so we can see the structure of the originalentitiesarray.