I have an API which returns an array of format:
[
{
"abc": "def",
"efg": "hij"
},
{
"abc": "def",
"efg": "hij"
}
]
When I hit the service with jquery.ajax I am able to get a response
$.ajax({
url: URL,
type: 'GET',
dataType: 'JSON',
xhrFields: {
withCredentials: false
},
success: function (data) {
console.log(data);
},
error: function (request, error) {});
On java script when I try to check the data type of data it is of type Object. When I directly print data on console
0: Object { "abc": "def", "egf":"ghi " }
1: Object { "abc": "def", "egf":"ghi " }
Where as when I print with knockout.toJSON(data) or JSON.stringify(data) it returns
[
{
"abc": "def",
"efg": "hij"
},
{
"abc": "def",
"efg": "hij"
}
]
However I am unable to convert the response into array as I want it to be an array.
I even tried JSON.parse on the knockout.JSON(data), JSON.stringify(data), and directly on data but it fails.
What is the best way to convert this to an array.
JS has primitive data types and Objects
,since Array
is not a primitive its typeof
will return Object
.
You can treat your response as an array in your code,no conversion is needed.
Study this SO post for further info and the docs about JS data types
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.