使用xamarin形式的api [POST]-序列化返回空的json对象

时间:2018-07-20 09:23:01

标签: post asp.net-web-api xamarin.forms httpclient

当我使用Newtonsoft将对象转换为json字符串时,Json返回空对象。

var json = JsonConvert.SerializeObject(user);

我一直得到这个结果“ {}” Register Form Screenshot

这些是我的密码

Registration.xaml

> <?xml version="1.0" encoding="utf-8" ?>
>     <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
>                  xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
>                  x:Class="FinalCustomerApp.Views.Registration"
>                  BackgroundImage="Background.png">
>     
>         <StackLayout Orientation="Vertical">
>     
>             <Label Text="Sign Up" FontAttributes="Bold" Font="50" > > >HorizontalOptions="Center" VerticalOptions="Center"/>
>             <StackLayout Orientation="Horizontal">
>                 <Label Text="Id" VerticalOptions="Center"/>
>                 <Entry  HorizontalOptions="FillAndExpand" x:Name="entId"/>
>             </StackLayout>
>     
>             <StackLayout Orientation="Horizontal">
>                 <Label Text="Username" VerticalOptions="Center"/>
>                 <Entry  HorizontalOptions="FillAndExpand" x:Name="entUsername"/>
>             </StackLayout>
>     
>             <StackLayout Orientation="Horizontal">
>                 <Label Text="Password" VerticalOptions="Center"/>
>                 <Entry  HorizontalOptions="FillAndExpand" x:Name="entPassword"/>
>             </StackLayout>
>     
>             <Button Text="Register" HorizontalOptions="Center" >x:Name="btnRegister" Clicked="btnRegister_Clicked"/>
>         </StackLayout>
>     </ContentPage>

Registration.xaml.cs

> public async void btnRegister_Clicked(object sender, EventArgs e)
>     {
>         Users user = new Users()
>         {
>             mem_account_no = entId.Text,
>             mem_acc_username = entUsername.Text,
>             mem_acc_password = entPassword.Text
>         };
> 
>            var json = JsonConvert.SerializeObject(user);
> 
>            var content = new StringContent(json, Encoding.UTF8, >"application/json");
> 
>            HttpClient client = new HttpClient();
> 
>            var result = await client.PostAsync("http://ropenrom24-001->site1.etempurl.com/potangina/final/Restserver/index.php/users/insert",
> content);
>          
> 
>         if (result.StatusCode == System.Net.HttpStatusCode.Created)
>         {
>             await DisplayAlert("Success", "Success", "OK");
>         }
>         else
>         {
>             await DisplayAlert("Failed", "My Json " + json, "OK");
>         }
> 
>         
>     } }

Users.cs

> public class Users {
>     public string mem_account_no { get; set; }
>     public string mem_acc_username { get; set; }
>     public string mem_acc_password { get; set; }
> 
>   }

1 个答案:

答案 0 :(得分:0)

我对JSON库不是很满意,但是我很确定您必须序列化JObject,而不是直接序列化Users对象...

每个JObject具有1个或多个JProperty,并带有一个名称。此名称将用于生成“序列化的” Json文本。

例如,您可以做类似的事情:

public class Users
{
    ... your properties ...

    public JObject ToJson()
    {
        return new JObject()
        {
            new JProperty("MemAccount", mem_account_no),
            new JProperty("MemUsername", mem_acc_username),
            new JProperty("MemPassword", mem_acc_password)
        }
    }
}

然后序列化您的用户,对您的JObject进行序列化,还可以在此过程中检查是否存在错误:

JsonSerializerSettings settings = new JsonSerializerSettings();
settings.Error = delegate(object sender, ErrorEventArgs args)
{
    /* manage your serialization errors here.
       Usefull for debugging
    */
};

var jsonContent = JsonConvert.SerializeObject(user.ToJson(), settings);

最后,在您的查询(REST API POST请求)中,这样设置内容:

var requestContent = new HttpStringContent(
    jsonContent, 
    Encoding.UTF8, 
    "application/json");

我忘了,如果要反序列化回对象,则必须在“用户”的属性上设置JsonProperty属性。

示例:

[JsonProperty("MemAccount")]
public string mem_account_no{ get; set; }

应该很好。告诉我是否清楚。