Problem with the way $resource deserializes a json representation of an array of strings.

3,088 views
Skip to first unread message

Carles Serrano

unread,
Apr 10, 2012, 11:44:15 AM4/10/12
to ang...@googlegroups.com
Hi guys

 I´m having problems deserializing an array of strings in json using $resource.

The json the server is sending is: 

["Name1","Name2","Name3"]

but $resources deserializes the json retrieved from the server like:


[["N","a","m","e","2"], ["N","a","m","e","2"], ["N","a","m","e","2"]]

This is how I call the $resource object.

$resource('/route/to/resource').query();

Here is a non working example in fiddle: 

http://jsfiddle.net/carpasse/96EU5/

Misko Hevery

unread,
Apr 10, 2012, 1:59:47 PM4/10/12
to ang...@googlegroups.com
Resources works on objects. So server needs to return an array of objects, not array of strings.

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To view this discussion on the web visit https://groups.google.com/d/msg/angular/-/_It7MqaBHREJ.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/angular?hl=en.

Misko Hevery

unread,
Apr 10, 2012, 2:00:12 PM4/10/12
to ang...@googlegroups.com
If you need to consume array of strings, then you need to use $http

Carles Serrano

unread,
Apr 11, 2012, 3:23:58 AM4/11/12
to ang...@googlegroups.com
Thanks for the clarification Misko.

I´ve changed to $http and everything works fine now.
Could you please explain why $resource only work with objects? 

Thanks again for the clarification and for the rapid response. This problem was driving me nuts.

Carles.

To unsubscribe from this group, send email to angular+unsubscribe@googlegroups.com.

zdam

unread,
Apr 11, 2012, 5:44:35 AM4/11/12
to ang...@googlegroups.com
You could write a responseInterceptor to convert your array to objects and then be able to use resource: (resource uses http so the response Interceptor would be invoked when using a resource) http://docs-next.angularjs.org/api/angular.module.ng.$http 

Misko Hevery

unread,
Apr 11, 2012, 11:28:58 AM4/11/12
to ang...@googlegroups.com
Resource is an OO wrapper over your data. For example Book would be a resource. So that you can do

var book = new Book();
book.save()

Since book is an object, having primitive types makes no sense.

To view this discussion on the web visit https://groups.google.com/d/msg/angular/-/RmELmEn1EeQJ.

To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.

Carles Serrano

unread,
Apr 12, 2012, 4:43:04 AM4/12/12
to ang...@googlegroups.com
It makes sense. Thanks for the explanation Misko.

Thanks for the idea about using interceptors zdam .
In the end I´m using $http in its simplest form beause its enough for me but the interceptors are an interesting  functionality.

Carles
Reply all
Reply to author
Forward
0 new messages