The server returns a string of JSON data to the client. In some cases, we do not know the original key value for some special reasons, so we cannot get the value through the key value as normal. In this case, we must read JSON object data cyclically. Let's take a look at the example. The JSON format string returned from the server end to the client:
Var str = '[{"uname": "Wang Qiang", "day": "2010/06/17" },{ "uname": "Wang haiyun", "day ": "2010/06/11"}] ';
We convert it to a JSON object: var jsonList = eval ("(" + str + ")"). At this time, if you use breakpoint tracking to view this jsonList object, you will actually find that, its length is 2, that is, jsonList. length = 2. Each of its items is a separate JSON object. See:
In this case, if we do not know the "uname" and "day" key values, we start to execute the loop operation on jsonList.
The Code is as follows:
For (var I = 0; I
For (var key in jsonList [I]) {
Alert ("key:" + key + ", value:" + jsonList [I] [key]);
}
}
In this way, we can easily obtain the key and value values we need.