, , , JSON . RootObject - .
JSON. , .
{
"url": "http://www.stackoverflow.com",
"display": "This is a test",
"genetics": [
"typeophere"
],
"price": [
"$1400"
],
"form": "a form"
}
RootObject. Pascal JSON.NET, /.
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
public class RootObject
{
[JsonProperty(PropertyName = "url")]
public string Url { get; set; }
[JsonProperty(PropertyName = "display")]
public string Display { get; set; }
[JsonProperty(PropertyName = "genetics")]
public List<string> Genetics { get; set; }
[JsonProperty(PropertyName = "price")]
public List<string> Price { get; set; }
[JsonProperty(PropertyName = "form")]
public string Form { get; set; }
}
-, , JSON . .
string json;
var resource = Application.GetResourceStream(new Uri("json.txt", UriKind.Relative));
using (var reader = new StreamReader(resource.Stream))
{
json = reader.ReadToEnd();
}
WebClient, JSON.
JSON RootObject.
var rootObject = JsonConvert.DeserializeObject<RootObject>(json);
. ListBox. ItemSource ListBox, , , . , rootObject . .
lstBoxResults.ItemsSource = rootObject;
. RootObject System.Collections.IEnumerable. , ItemsSource.
. .
lstBoxResults.ItemsSource = new List<RootObject> { rootObject };
, JSON rootObject. ListBox .
, JSON RootObjects. , , "", RootItems.
{
"items": [
{
"url": "http://www.stackoverflow.com",
"display": "This is a test",
"genetics": [ "typeophere" ],
"price": [ "$1400" ],
"form": "a form"
},
{
"url": "http://cgeers.com",
"display": "This is another test",
"genetics": [ "typeophere" ],
"price": [ "$2000" ],
"form": "another form"
}]
}
Deserialing . JSON.NET RootObjects.
var data = JObject.Parse(json)["items"];
(IEnumerable).
var rootObjects = JsonConvert.DeserializeObject<IEnumerable<RootObject>>(data.ToString());
RootObject, . ListBox.
lstBoxResults.ItemsSource = rootObjects;
, JSON, , . , , JSON.
:
json = json.Substring(json.IndexOf("[") + 1);
json = json.Substring(0, json.LastIndexOf("]"));
var rootObjects = JsonConvert.DeserializeObject<RootObject>(json);