Xamarin表单:如何解析项目中存储的本地JSON数据文件



我有一个本地JSON文件。我需要解析该文件中的数据,并需要一个包含日期、颜色和消息详细信息的列表。

JSON文件中的数据格式:

{"01-01-2017":{"color":"white","message":"The Octave Day of Christmas Solemnity of the Blessed Virgin Mary, the Mother of God Lectionary: 18"},"02-01-2017":{"color":"white","message":"Memorial of Saints Basil the Great and Gregory Nazianzen, Bishops and Doctors of the Church Lectionary: 205",.......}}

型号类别

public class JsonContent
{
public string date { get; set; }
public string color { get; set; }
public string message { get; set; }
}

我尝试了以下代码:

string jsonString;
string jsonFileName = "Files.prayers.json";
var assembly = typeof(HomePage1).GetTypeInfo().Assembly;
Stream stream = assembly.GetManifestResourceStream($"{assembly.GetName().Name}.{jsonFileName}");
using (var reader = new System.IO.StreamReader(stream))
{
jsonString = reader.ReadToEnd();
}
List <JsonContent> newList = new List<JsonContent>();
//I need to add all the items into the above list

如何获取包含日期、颜色和消息详细信息的列表?之后,我需要在XamForms.Enhanced.Calendar上用颜色显示日期。我知道如何在日历中显示特殊日期,但一直在进行数据分析。

docs

我想试试这种

Dictionary<string, JsonContent> jsonData = JsonConvert.DeserializeObject<Dictionary<string, JsonContent>>(jsonString);
foreach (var item in jsonData)
{
Debug.WriteLine("Date:>>" + item.Key);
Debug.WriteLine("color:>>" + item.Value.color);
Debug.WriteLine("message:>>" + item.Value.message);
}

最新更新