Xamarin forms: How to parse local JSON data file stored in the project?

Sreejith Sree 1,251 Reputation points
2021-12-14T15:20:19.167+00:00

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.

Developer technologies .NET Xamarin
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2021-12-15T02:23:32.627+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    using JsonConvert.DeserializeObject<Dictionary<string, JsonContent>> to deserialize the JSON data

       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);  
        }  
    

    Or change the JSON type, JSON data need key-value format, but 01-01-2017 do not have a key. Based on your JsonContent.cs, your JSON file need like this

       {  
          "JsonContent":[  
             {"date":"01-01-2017","color":"white","message":"The Octave Day of Christmas Solemnity of the Blessed Virgin Mary, the Mother of God Lectionary: 18"},  
             {"date":"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 need add Root.cs like following code.

       public class JsonContent  
           {  
               public string date { get; set; }  
               public string color { get; set; }  
               public string message { get; set; }  
           }  
         
           public class Root  
           {  
               public List<JsonContent> JsonContent { get; set; }  
           }  
    

    You can DeserializeObject with Newtonsoft.Json nuget package.

       Root data = JsonConvert.DeserializeObject<Root>(jsonString);  
        List<JsonContent> jsonContents= data.JsonContent;  
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Sreejith Sree 1,251 Reputation points
    2021-12-15T08:41:10.763+00:00

    @Anonymous Got solution for parsing:

    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);  
    }
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.