简体   繁体   English

如何将数组作为参数发送给请求主体?

[英]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. 我不知道如何将UserCategories作为数组发送,因为它不是String,所以可以像其他参数一样发送它来构造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! 我真的很困惑!

When you send or receive JSON data, it is always as a string . 在发送或接收JSON数据时, 它始终是string We might speak of JSON objects or JSON arrays, but they are really string representations of objects and arrays. 我们可能会说JSON对象或JSON数组,但它们实际上是对象和数组的字符串表示形式 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. 发送方应从数组创建一个字符串,接收方应将JSON字符串解析为一个数组。 There are many libraries available to help you do this in every language. 有许多可用的库来帮助您使用每种语言进行此操作。

your existing UserCategories object is List/Array format after that changed in String format so only its happen 您现有的UserCategories对象在更改为String格式后为List / Array格式,因此仅会发生

"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":"[]" 这是您的第二次Json,在这里看到您的列表以“”发送,例如"UserCategories":"[]"

Yes. 是。 You can do that by using retrofit library. 您可以通过使用改造库来实现。 You can sent object,arraylist using Retrofit. 您可以使用Retrofit发送对象,数组列表。 https://futurestud.io/tutorials/retrofit-send-objects-in-request-body.Check this url to sent Object. https://futurestud.io/tutorials/retrofit-send-objects-in-request-body。检查此网址以发送对象。 You can also sent arraylist under Object In body 您还可以在Object In正文下发送arraylist

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM