В настоящее время я пытаюсь прочитать Json-файл (named initial_data.json), который находится в папке DataModels в папке Project. Проблема, с которой я столкнулась, это не будет читать файл. Я пробовал следующий код для чтения файла в json string (который я буду использовать для десериализации позже, поэтому я попытался отобразить его в resultTextBlock), и ничего не получилось. Вот мой Json формат файлаЧтение/десериализация json файла в WP8.1 C#
{
"brands": [
{
"id": "AUD",
"name": "AUDI",
"sort": "99",
"active": true
},
{
"id": "BEN",
"name": "MERCEDES-BENZ",
"sort": "6",
"active": true
},
{
"id": "BMW",
"name": "BMW",
"sort": "7",
"active": true
},
{
"id": "CHE",
"name": "CHEVROLET",
"sort": "8",
"active": true
}
],
"models": [
{
"id": "100",
"name": "CIVIC",
"brandID": "HON",
"size": null,
"year": "-",
"active": true
},
{
"id": "101",
"name": "CRV",
"brandID": "HON",
"size": null,
"year": "-",
"active": true
},
{
"id": "102",
"name": "CRVEXI",
"brandID": "HON",
"size": null,
"year": "-",
"active": true
},
{
"id": "103",
"name": "GDYSSEY",
"brandID": "HON",
"size": null,
"year": "-",
"active": true
}
]
}
Вот мой JSON Класс
public class Brand
{
[JsonProperty("id")]
public string id { get; set; }
[JsonProperty("name")]
public string name { get; set; }
[JsonProperty("sort")]
public string sort { get; set; }
[JsonProperty("active")]
public bool active { get; set; }
[JsonProperty("path")]
public string path { get; set; }
}
public class Model
{
[JsonProperty("id")]
public string id { get; set; }
[JsonProperty("name")]
public string name { get; set; }
[JsonProperty("brandID")]
public string brandID { get; set; }
[JsonProperty("size")]
public object size { get; set; }
[JsonProperty("year")]
public string year { get; set; }
[JsonProperty("active")]
public bool active { get; set; }
}
А вот как я пишу функцию чтения
private async Task readJsonAsync()
{
// Notice that the write **IS** identical ... except for the serializer.
string content = String.Empty;
var myStream = await ApplicationData.Current.LocalFolder.OpenStreamForReadAsync(JSONFILENAME);
using (StreamReader reader = new StreamReader(myStream))
{
content = await reader.ReadToEndAsync();
}
resultTextBlock.Text = content;
}
Любая помощь будет оценена
@ Schwin W Pinkoh ваша проблема решена? – Joseph
Спасибо, что вернулись! Я блуждал, но все еще не мог найти этот метод OpenRead. На самом деле я не смог найти «Файл». Когда я использую ваш данный код, он говорит, что имя «Файл» не существует в текущем методе. (Я уже набрал System.IO; вверху) –