0

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
3
  • 1
    What does entities look like in its original state? If it is an array, you should not be using for in, whose purpose is iterating object properties. Commented Nov 24, 2012 at 20:41
  • Thanks Michael. it is a array which is returned by my database query. can you pl let me know how to do that Commented Nov 24, 2012 at 20:48
  • 1
    Do console.log(JSON.stringify(entities)) so we can see the structure of the original entities array. Commented Nov 24, 2012 at 20:49

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.