Ozkan Ciftci
Ozkan Ciftci

Reputation: 89

How to get length of json object array?

I wrote a restService and my service returns me two different result :

{"Response": {
"cars":[
{"id":"1",
"name":"test1"},
{"id":"2",
"name":"test2"},
{"id":"3",
"name":"test3"}
],
"books":[
{"id":"4",
"name":"example1"},
{"id":"5",
"name":"example2"},
{"id":"6",
"name":"example3"}    
]}}

and second ,

{"Response": {
"cars":{
"id":"1",
"name":"test1"},
"books":{
"id":"4",
"name":"example1"}
}}

I will show the result in my dataTable with javascript. For this, I want to learn length of cars or books. If result >1 I can get length, cars or books act as an array,but if result <1 it acts as an object and I can't get length of objects .In my restService, I defined as an ArrayList the cars and books. Is there any solutions for this ? Thanks for responses.

Upvotes: 0

Views: 875

Answers (1)

folkol
folkol

Reputation: 4883

The problem lies with your REST Service, make it always return a list of cars, even if there is only one car.

Upvotes: 3

Related Questions