简体   繁体   English

xamarin.forms-Newtonsoft.Json.JsonSerializationException:转换值时出错

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

I'm new to Xamarin.Forms. 我是Xamarin.Forms的新手。 I`m trying to deserialize JSON string to display to into a listview. 我正在尝试反序列化JSON字符串以显示到列表视图中。 I succeeded to get the JSON string from the server but when trying to deserialize it, it throws this error: 我成功地从服务器获取了JSON字符串,但是在尝试反序列化时,会引发以下错误:

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

Here is my code: 这是我的代码:

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; }
        }

    }
}

The json string: json字符串:

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

Any idea of what am I doing wrong? 知道我在做什么错吗?

Your JSON property "titulo" is a string not an array of Place. 您的JSON属性“ titulo”是一个字符串,而不是Place数组。 The json object would need to be: json对象需要为:

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

暂无
暂无

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

相关问题 Xamarin Newtonsoft.Json.JsonSerializationException: - Xamarin Newtonsoft.Json.JsonSerializationException: 错误:Newtonsoft.Json.JsonSerializationException - Error: Newtonsoft.Json.JsonSerializationException Newtonsoft.Json.JsonSerializationException - Newtonsoft.Json.JsonSerializationException Newtonsoft.Json.JsonSerializationException:转换值时出错,无法从 System.String 转换或转换为 DashBoard.DashboardReport - Newtonsoft.Json.JsonSerializationException: Error converting value, Could not cast or convert from System.String to DashBoard.DashboardReport 抛出了Newtonsoft.Json.JsonSerializationException - Xamarin - Newtonsoft.Json.JsonSerializationException has been thrown - Xamarin Newtonsoft.Json.JsonSerializationException(从'System.Data.SqlTypes.SqlDouble'的'Value'获取值时出错)序列化SqlGeography - Newtonsoft.Json.JsonSerializationException (Error getting value from 'Value' on 'System.Data.SqlTypes.SqlDouble) serializing SqlGeography Newtonsoft.Json.JsonSerializationException:从“Castle.Proxies.SalonProxy”上的“MyEntity”获取值时出错 - Newtonsoft.Json.JsonSerializationException: Error getting value from 'MyEntity' on 'Castle.Proxies.SalonProxy' ASP.NET Core API with WPF front-end „Newtonsoft.Json.JsonSerializationException” w mscorlib.dll Error converting value "Value cannot be null - ASP.NET Core API with WPF front-end „Newtonsoft.Json.JsonSerializationException” w mscorlib.dll Error converting value "Value cannot be null 用户代码未处理Newtonsoft.Json.JsonSerializationException - Newtonsoft.Json.JsonSerializationException was unhandled by user code Twitterizer TwittterTimeline NewtonSoft.JSON.JsonSerializationException问题 - Twitterizer TwittterTimeline NewtonSoft.JSON.JsonSerializationException problems
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM