0

我正在从数据库中读取数据:我有我想要的值

    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);
    }

以上将打印我想要的所有数据。现在我想将其转换为 Json 对象。我怎样才能做到这一点 。

我已经使用并知道我可以使用 JSON.stringify 但是当我在这种情况下在这里尝试时,它会给出错误。

我试着保持在forloop中:

jasonarray[index1].PartitionKey     = entities[index].PartitionKey;
jasonarray[index1].RowKey           = entities[index].RowKey;

JSON.stringify({ key: jasonarray[index1].PartitionKey, RowKey: jasonarray[index1].RowKey )

但是在执行这个函数时它是未定义的。

我正在寻找的只是var x:

where x is a 

[

{partionkey: "val",key: "val"}
{partionkey: "val",key1: "val"}
{partionkey: "val",key2: "val"}

]
4

1 回答 1

2

Javascript:工作示例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));

​或 C#:

string json = (new JavascriptSerializer()).Serialize(entities.Select(x => new
    {
        x.PartitionKey,
        x.RowKey
    }
));
于 2012-11-24T20:58:10.637 回答