Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have this json file located in my Assets/Dec1.js .
How can i make it appear in my textblock?
When i run it in the emulator it shows "This" instead of showing the content in the Assets/Dec1.js


xaml
HTML
<ScrollViewer>
                <TextBlock x:Name="textblock" TextWrapping="Wrap" Foreground="White"/>
            </ScrollViewer>



xaml.cs
C#
//JSON starts here
public string ReadJsonFile(string JsonfilePath)
{
    string strText = "This";

    using (StreamReader r = new StreamReader("Assets/Dec1.js"))
    {

        string json = r.ReadToEnd();
        dynamic array = JsonConvert.DeserializeObject(json);
        //... read text from json file


    }

    return strText;

}

//Daily Devotion starts here
protected override void OnNavigatedTo(NavigationEventArgs e)
{

    AddDevotions();
    int index = DateTime.Now.DayOfYear;
    textblock.Text = devotions[index];
}

List<String> devotions = new List<string>();
private void AddDevotions()
{
devotions.Add(ReadJsonFile("Assets/Dec1.js"));
    devotions.Add(ReadJsonFile("Assets/Dec1.js"));
    devotions.Add(ReadJsonFile("Assets/Dec1.js"));
    }



Thank you in advance and reply soon
Posted
Updated 16-Jan-15 5:17am
v3
Comments
Afzaal Ahmad Zeeshan 16-Jan-15 10:24am    
.js is not a JSON file brother, it is a JavaScript script file. You should change that to .json.
Member 10627743 16-Jan-15 10:41am    
When i changed the file to .json this is the error given "An exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.ni.dll but was not handled in user code

If there is a handler for this exception, the program may be safely continued."

Kindly proffer a solution

Thank you
Afzaal Ahmad Zeeshan 16-Jan-15 10:56am    
That is because you are required to change the File name in the Code too...
Member 10627743 16-Jan-15 11:07am    
Hi Afzaal Ahmad Zeeshan

Sorry to bother you, i have changed all the extension see the code below

xaml.cs
//JSON starts here
public string ReadJsonFile(string JsonfilePath)
{
string strText = "This";

using (StreamReader r = new StreamReader("Assets/Dec1.json"))
{

string json = r.ReadToEnd();
dynamic array = JsonConvert.DeserializeObject(json);
//... read text from json file


}

return strText;

}

//Daily Devotion starts here
protected override void OnNavigatedTo(NavigationEventArgs e)
{

AddDevotions();
int index = DateTime.Now.DayOfYear;
textblock.Text = devotions[index];
}

List<string> devotions = new List<string>();
private void AddDevotions()
{
devotions.Add(ReadJsonFile("Assets/Dec1.json"));
devotions.Add(ReadJsonFile("Assets/Dec1.json"));
devotions.Add(ReadJsonFile("Assets/Dec1.json"));
}

What else am i doing wrong kindly help
Afzaal Ahmad Zeeshan 16-Jan-15 11:09am    
So, what is the error here now? Can you share that error also?

1 solution

Working with JSON data in a file is just as reading the file resource, and then in the JSON file you just deserialize the data to create an object of that data, it can be anonymous or, you can create a special (custom) class for working with data coming through the file. Supposing that you're going to store the data for Daily data. You can create a class as,

C#
class Devotion {
   public int DevotionID { get; set; }
   public int DevotionLabel { get; set; }
   // all other properties
}


.. then you can use the Newtonsoft.JSON API to create the JSON notation for the data. For example, to read the objects, you should create a list of these objects as,

C#
string jsonData = File.ReadAllText("/file.json"); // or that r.ReadToEnd()
List<devotion> devotions = new List<devotion>();
// make sure that the data in file starts with [ and ends at ]; because of List object
devotions = JsonConvert.DeserializeObject<list><devotion>>(jsonData);
</devotion></list></devotion></devotion>


Now the devotions objects is an enumerable containing all of your daily devotions. You can use a foreach loop, to use each of these objects and write their values in TextBlock. Same method is used to save the values in the JSON file.

You can learn more from these...

JSON Serialization Using Newtonsoft JSON Serialize[^]

Documentation of JSON.NET[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900