F.Mi
F.Mi

Reputation: 51

How can I send an Array as a parameter for making body for requesting to the server?

I have to send some parameters for making body for requesting to the server. like this:

"UserName":"a.m",
"CityId":"1",
"UserCategories":
[
  {"CategoryId":"ab2d948a-59d1-420d-a29b-8fd88c2d637c"},
  {"CategoryId":"237b6334-3c1f-44ac-bd87-a8e6be0b2144"}
]

I don't know how can I send UserCategories as an array.because it is not String that I can send it like other parameters for making body. I should have the body like this for getting response:

body: {"CityId":"1","UserName":"a.m","UserCategories":[{"UserCategories":"4211f3f4-f506-4458-b96a-0b496515e019"},{"UserCategories":"df7487b3-2043-46ec-97d4-790bfbe83cfc"}]}

but now my body is:

body: {"CityId":"1","UserName":"a.m","UserCategories":"[{"UserCategories":"4211f3f4-f506-4458-b96a-0b496515e019"},{"UserCategories":"df7487b3-2043-46ec-97d4-790bfbe83cfc"}]"}

I'm really confused!

Upvotes: 0

Views: 98

Answers (3)

Code-Apprentice
Code-Apprentice

Reputation: 83527

When you send or receive JSON data, it is always as a string. We might speak of JSON objects or JSON arrays, but they are really string representations of objects and arrays. You can send the data exactly as you have written here as long as you enclose the entire thing in {}. You can send just the array in string form if you wish, exactly as you typed it here. The sender should create a string from the array and the receiver should parse the JSON string into an array. There are many libraries available to help you do this in every language.

Upvotes: 1

Shohel Rana
Shohel Rana

Reputation: 2342

Yes. You can do that by using retrofit library. You can sent object,arraylist using Retrofit. https://futurestud.io/tutorials/retrofit-send-objects-in-request-body.Check this url to sent Object. You can also sent arraylist under Object In body

Upvotes: 0

Raja
Raja

Reputation: 2815

your existing UserCategories object is List/Array format after that changed in String format so only its happen

"UserCategories":"[
    {"UserCategories":"4211f3f4-f506-4458-b96a-0b496515e019"},
    {"UserCategories":"df7487b3-2043-46ec-97d4-790bfbe83cfc"}]"

this is your second-time Json, Here see your List sent with "" like "UserCategories":"[]"

Upvotes: 0

Related Questions