开发者

Help with JSON structure

I have the following javascript which I need to make dynamic.

data.addRows(2);
data.setValue(0, 0, 'name goes here');
data.setValue(0, 1, value 1 goes here); 
data.setValue(0, 2, value 2 goes here); 
data.setValue(1, 0, 'name goes here');
data.setValue(1, 1, value 1 goes here); 
data.setValue(1, 2, value 2 goes here); 

I guess a loop is the best way to go forward:

I have come up with the following json structure:

[ 
    {"User1":{"value1": 50.00,"value2": "100"}}, 
    {"User2":{"value1": 10.00,"value2": "20"}}, 
    {"User3":{"value1": 10.00,"value2": "20"}}, 
    {"User4":{"value1": 10.00,"value2": "20"}}, 
    {"User5":{"value1": 20.00,"value2": "40"}} 
] 
开发者_如何学Python

I think this structure needs improving. Can someone please suggest a better structure to make it very easy for me to extract the data I want to extract from this?


var dataset = [
  {uid: 'user1', value1: 50.00, value2: 100},
  {uid: 'user2', value1: 10.00, value2: 20}, 
];

That way you can do data.length to figure out how many values you have and you can loop a bit easier. For example:

for(i = 0; i < dataset.length; i++) {
   console.log(data[i].uid);
}

Using your example:

data.addRows(2); 
var l = dataset.length, i, x = 0, y = 0;
for(i = 0; i < l; i++) {
  data.setValue(y, x, dataset[i].uid); x++;
  data.setValue(y, x, dataset[i].value1); x++;
  data.setValue(y, x, dataset[i].value2); x=0;
  y++;
}


I don't see that you could make it anymore simple, unless there's something special about the data that would allow repeated parts to be separated out in some fashion.


Try something like:

{"users": [
    {"value1": 50, "value2": 100},
    {"value1": 10, "value2": 20},
    {"value1": 10, "value2": 20},
    {"value1": 10, "value2": 20},
    {"value1": 20, "value2": 40}
]}

Or, if you want to have your users have IDs:

{"users": {
    "userId1": {"value1": 50, "value2": 100},
    "userId2": {"value1": 10, "value2": 20},
    "userId3": {"value1": 10, "value2": 20},
    "userId4": {"value1": 10, "value2": 20},
    "userId5": {"value1": 20, "value2": 40}
}}

Remember, all numbers in JavaScript are floating-point numbers.


I think you can try something like this, the idea is from ColdFusion SerializeJson()

I assume your data is like a table

user        value1        value2
------------------------------------
user1       50            100
user2       10            20
...

the json structure is

{"COLUMNS":["USER","VALUE1","VALUE2"],"DATA":[["user1",50.0,100.0],["user2",10.0,20.0]]} 

more clear reading

{
    "COLUMNS":
        [
            "USER",
            "VALUE1",
            "VALUE2"
        ],
    "DATA":
        [
            [
                "user1",
                50.0,
                100.0
            ],
            [
                "user2",
                10.0,
                20.0
            ]
        ]
}

with this structure, we know that every varname.DATA[index][0] is refers to USER


Check this link for json encoding

And for now your json value should be encoded like this

{"id":8,"heading":"heading goes here","content":"&lt;p&gt;content goes here&lt;\/p&gt;"}

Sorry not for you....

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜