0

I have a local JSON file. I need to parse data from that file and need a list with date,color and message details.

Data format in JSON file:

{"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",.......}}

Model Class

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

I tried the below code:

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

How can I get the list with date,color and message details? After this I need to show the dates with color on a XamForms.Enhanced.Calendar. I know how to show special dates in calendar, but stuck on this data parsing.

2
  • json2csharp.com Commented Dec 14, 2021 at 12:47
  • @Jason We have updated the question with more details, could you please have a look? Commented Dec 14, 2021 at 15:20

1 Answer 1

2

docs

I'd try something like this

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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

From this dictionary how I can parse the date, color and message? Could you share a sample ode for that?
color and message are properties of the JsonContent class you created. The date is the key of the dictionary. I don't know if there is an easy way to automatically parse that. You might have to post-process the data to convert the date.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.