Na chwilę obecną pobieram sobie tekst JSON korzystając z kodu:
private async void button9_Click(object sender, EventArgs e)
{
string uri = "http://www.halfordsautocentres.com/webapp/wcs/stores/servlet/Gateway?action=findStores&responseType=json&storeId=11602&latitude=52.2719517&longitude=-1.8789762999999766&storeType=1";
HttpClient h = new HttpClient();
string r = await h.GetStringAsync(uri);
richTextBox2.Text = r;
}
Potrzebują z niego uzyskać informację adresowe. z pomocą Google udało mi się odszukać parsery online, które pokazują czytelną strukturę.
Znalazłem też http://json2csharp.com/ gdzie otrzymałem w wyniku kilka klas C#:
public class Attribute
{
public string altText { get; set; }
public List<object> derivedAttributes { get; set; }
public string description { get; set; }
public bool facility { get; set; }
public bool facilityEnabled { get; set; }
public string image { get; set; }
public string name { get; set; }
public bool service { get; set; }
public bool serviceEnabled { get; set; }
public bool storeType { get; set; }
public bool superAttribute { get; set; }
public string value { get; set; }
}
public class StorePagesUrl
{
public bool categoriesView { get; set; }
public int categoryId { get; set; }
public bool homeView { get; set; }
public bool listView { get; set; }
public int productId { get; set; }
public string spfId { get; set; }
public bool storeView { get; set; }
public string viewId { get; set; }
}
public class HighStreetStoreDetail
{
public string address1 { get; set; }
public string address2 { get; set; }
public List<Attribute> attributes { get; set; }
public string brand { get; set; }
public bool collectionSupported { get; set; }
public string countryCode { get; set; }
public string county { get; set; }
public double distance_in_miles { get; set; }
public bool emergency { get; set; }
public string emergencyText { get; set; }
public string fax { get; set; }
public bool fridayClosed { get; set; }
public string fridayClosingTime { get; set; }
public string fridayOpeningTime { get; set; }
public List<object> holidays { get; set; }
public string id { get; set; }
public double latitude { get; set; }
public double longitude { get; set; }
public string managerId { get; set; }
public string managerName { get; set; }
public bool mondayClosed { get; set; }
public string mondayClosingTime { get; set; }
public string mondayOpeningTime { get; set; }
public string name { get; set; }
public string postcode { get; set; }
public bool saturdayClosed { get; set; }
public string saturdayClosingTime { get; set; }
public string saturdayOpeningTime { get; set; }
public string shortName { get; set; }
public string storeDetailsPageUrl { get; set; }
public StorePagesUrl storePagesUrl { get; set; }
public bool sundayClosed { get; set; }
public string sundayClosingTime { get; set; }
public string sundayOpeningTime { get; set; }
public string telephone { get; set; }
public bool thursdayClosed { get; set; }
public string thursdayClosingTime { get; set; }
public string thursdayOpeningTime { get; set; }
public string town { get; set; }
public bool tuesdayClosed { get; set; }
public string tuesdayClosingTime { get; set; }
public string tuesdayOpeningTime { get; set; }
public bool tyreFitAvailable { get; set; }
public bool wednesdayClosed { get; set; }
public string wednesdayClosingTime { get; set; }
public string wednesdayOpeningTime { get; set; }
}
public class Status
{
public int code { get; set; }
public string message { get; set; }
}
public class RootObject
{
public List<HighStreetStoreDetail> highStreetStoreDetails { get; set; }
public double originLatitude { get; set; }
public double originLongitude { get; set; }
public Status status { get; set; }
}
Mam tu uwzględnione kolejno wszystkie pola z tego JSON, tylko jak mam tego użyć.
Używam do tego VS Express 2013 for Desktop [C#]. Wersja .NET zapewne 4.5.
Spróbowałem tak i na debuggerze coś wyszło nawet:
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(RootObject));
RootObject user = (RootObject)serializer.ReadObject(GetStream(richTextBox2.Text));
Problem jam jak się dobrać do poszczególnych elementów w tym drzewku, czyli dla mnie ważny jest adres.
string name = user.highStreetStoreDetails.Count.ToString();
/// <summary>
/// Get Byte[] from String
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static byte[] GetBytes(string str)
{
byte[] bytes = new byte[str.Length * sizeof(char)];
System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}
/// <summary>
/// Get Stream from String
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static Stream GetStream(string str)
{
return new MemoryStream(GetBytes(str));
}
Dzięki za podpowiedź.