簡體   English   中英

xamarin.forms-Newtonsoft.Json.JsonSerializationException:轉換值時出錯

[英]xamarin.forms - Newtonsoft.Json.JsonSerializationException: Error converting value

我是Xamarin.Forms的新手。 我正在嘗試反序列化JSON字符串以顯示到列表視圖中。 我成功地從服務器獲取了JSON字符串,但是在嘗試反序列化時,會引發以下錯誤:

Newtonsoft.Json.JsonSerializationException: Error converting value "El Rey Cantina TRC" to type 'App7.Page1+Place[]'. Path 'titulo', line 1, position 30.

這是我的代碼:

namespace App7
{
    public partial class Page1 : ContentPage
    {
        static ListView lstPlaces = new ListView();

        public Page1()
        {

            //borrar
            Button newButn = new Button()
            {

                Text = "Connect to Service",
                TextColor = Color.FromHex("#333333"),
                HorizontalOptions = LayoutOptions.Center

            };

            Content = new StackLayout
            {

                Children = {

                    newButn,
                    lstPlaces

                }

            };
            //borrar

            //click
            newButn.Clicked += newButn_Clicked;
            lstPlaces.ItemTemplate = new DataTemplate(typeof(TextCell));
            lstPlaces.ItemTemplate.SetBinding(TextCell.TextProperty, "titulo");
            //click

        }

        async void newButn_Clicked(object sender, EventArgs e)
        {
            //que pedo
            GeoNamesWebService geoService = new GeoNamesWebService();
            Place[] places = await geoService.GetPlacesAsync();
            lstPlaces.ItemsSource = places;
        }

        public class GeoNamesWebService
        {
            public GeoNamesWebService()
            {
            }
            public async Task<Place[]> GetPlacesAsync()
            {
                var client = new System.Net.Http.HttpClient();
                client.BaseAddress = new Uri("http://www.catcherapp.net/");
                StringContent str = new StringContent("postalcode=752020&country=IN&username=nirmalh", Encoding.UTF8, "application/x-www-form-urlencoded");
                var response = await client.PostAsync(new Uri("http://www.catcherapp.net/borrar/borrar.php"), str);
                var placesJson = response.Content.ReadAsStringAsync().Result;
                Placeobject placeobject = new Placeobject();
                if (placesJson != "")
                {
                    placeobject = JsonConvert.DeserializeObject<Placeobject>(placesJson);
                }
                return placeobject.places;
            }
        }


        public class Placeobject
        {
            [JsonProperty("titulo")]
            public Place[] places { get; set; }
        }
        public class Place
        {
            public string placeName { get; set; }
        }

    }
}

json字符串:

{"titulo":"Bistro Garden","idE":"gb54ezpjs9k0es8w5q","pp":"sge39na6rbpp7uudgk.jpg","direccion":"Feliciano Cobian #570, Col. Nueva Los Angeles, Torre\u00f3n","contador":null}

知道我在做什么錯嗎?

您的JSON屬性“ titulo”是一個字符串,而不是Place數組。 json對象需要為:

{ 
   "titulo":[
         {"placeName":"place1"},
         {"placeName":"place2"}]
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM