0
저는 Xamarin.Forms를 처음 사용합니다. JSON 문자열을 deserialize하여 listview에 표시하려고합니다. 나는 서버에서 JSON 문자열을 얻기 위해 성공했지만 직렬화 복원하려고 할 때,이 오류가 발생합니다 :xamarin.forms - Newtonsoft.Json.JsonSerializationException : 값을 변환하는 중 오류가 발생했습니다.
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}
다음
Newtonsoft.Json.JsonSerializationException: Error converting value "El Rey Cantina TRC" to type 'App7.Page1+Place[]'. Path 'titulo', line 1, position 30.
내 코드입니다
내가 뭘 잘못하고 있는지 알기 원하십니까?
거기에 json 문자열을 사용하는 형식이 있습니까? –
@GustavoSerna는 'PlaceObject'속성 'places'를 문자열로 변경합니다. – ATerry