Hi
I have this jason 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
Kindly help
when i run it in the emulator it shows "This" instead of showing the content in the Assets/Dec1.js
Kindly help
see the code below
xaml
xaml.cs
//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
dynamic array = JsonConvert.DeserializeObject(json);
//... read text from json file
}
return strText;
}
}
//Daily Devotion starts here
protected override void OnNavigatedTo(NavigationEventArgs e)
{
protected override void OnNavigatedTo(NavigationEventArgs e)
{
AddDevotions();
int index = DateTime.Now.DayOfYear;
textblock.Text = devotions[index];
}
int index = DateTime.Now.DayOfYear;
textblock.Text = devotions[index];
}
List devotions = new List();
private void AddDevotions()
{
devotions.Add(ReadJsonFile("Assets/Dec1.js"));
devotions.Add(ReadJsonFile("Assets/Dec1.js"));
devotions.Add(ReadJsonFile("Assets/Dec1.js"));
}
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
VulpesPosted Jan 19, 2015, 9:50 AM
and then change this line:
VulpesPosted Jan 19, 2015, 10:22 AM
If you just click the "Accepted Answer" button on my last post , that gives me more points (not that I need them!) but, more importantly, indicates to anyone else reading the thread with a similar problem that there is an answer here to it.
JOHN JOHNNNYPosted Jan 19, 2015, 10:14 AM
All the content of the page shows now.
Is there a place apart from the thumb up that i can vote/give you stars? if there is let me know for me to show my appreciation
Once again thank you
JOHN JOHNNNYPosted Jan 19, 2015, 9:13 AM
Finally part of the content in json shows on the emulator
But i will still have to ask you
According to the json sample i pasted above, it is only the message that shows others like date, title, memorise, read chapter and note did not appear on the emulator page
How can i make all the content to show, kindly help
VulpesPosted Jan 19, 2015, 8:45 AM
Are all 366 files set up in this way?
JOHN JOHNNNYPosted Jan 19, 2015, 8:00 AM
+$exception{Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'DevotionJson.Devotion' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List
Path '', line 1, position 1.
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureArrayContract(JsonReader reader, Type objectType, JsonContract contract)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
at Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType)
at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value)
at DevotionJson.MainPage.ReadJsonFile(String JsonfilePath)
at DevotionJson.MainPage.AddDevotions()
at DevotionJson.MainPage..ctor()}System.Exception {Newtonsoft.Json.JsonSerializationException}
What is the solution to this
JOHN JOHNNNYPosted Jan 17, 2015, 12:56 PM
VulpesPosted Jan 17, 2015, 11:52 AM
The error message suggests that it can't find (at least) one of these files and may be you haven't created them all yet?
Incidentally, it's struck me that we are going to need some more code to deal with February 29 which will be day 60 in a leap year. However, in a non-leap year day 60 will be 1 March.
I think this revised code should do it:
JOHN JOHNNNYPosted Jan 17, 2015, 7:49 AM
this "An exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.ni.dll but
was not handled in user code" what does this mean
Sorry for bothering you
VulpesPosted Jan 17, 2015, 7:19 AM
Instead of:
d = JsonConvert
it should be:
JOHN JOHNNNYPosted Jan 17, 2015, 7:07 AM
Yes i omitted it and i have included it by making it public, but when i ran the build it shows the following error
"Error 1 The non-generic type 'Newtonsoft.Json.JsonConvert' cannot be used with type arguments"
What does this mean, kindly help
Thank you and reply soon
VulpesPosted Jan 16, 2015, 6:00 PM
JOHN JOHNNNYPosted Jan 16, 2015, 4:37 PM
Thank you for the piece of code and going through all the stress i am very grateful.
After following your procedure and i ran the build it shows the following error
"Inconsistent accessibility: return type 'DevotionJson.Devotion' is less accessible than method 'DevotionJson.MainPage.ReadJsonFile(string)' "
Kindly help and reply soon
VulpesPosted Jan 16, 2015, 12:31 PM
public class Devotion
{
public string Date {get; set;}
public string Title {get; set;}
public string Verse {get; set;}
[JsonProperty("Read Chapter")]
public string ReadChapter {get; set;}
[JsonProperty("Read Text")]
public string ReadText {get; set;}
[JsonProperty("Bible In One Year")]
public string BibleInOneYear {get; set;}
public string Message {get; set;}
public string Notes {get; set;}
}
Assuming, there's one of these objects per file and there are 366 files, one for each day of the year (including leap years), you can then do this:
private List
public App()
{
InitializeComponent();
// as this will be expensive to populate
// you only want to do it once when the app starts
devotions = new List
AddDevotions();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
int index = DateTime.Now.DayOfYear - 1; // list is indexed from 0
textblock.Text = devotions[index].Message; // or some other property
}
private void AddDevotions()
{
for(int i = 1; i <= 366; i++)
{
string filePath = "Assets/Dec" + i.ToString() + ".js";
Devotion d = ReadJsonFile(filePath);
devotions.Add(d);
}
}
public Devotion ReadJsonFile(string JsonfilePath)
{
Devotion d = null;
using (StreamReader r = new StreamReader(JsonfilePath))
{
string json = r.ReadToEnd();
d = JsonConvert
}
return d;
}
JOHN JOHNNNYPosted Jan 16, 2015, 11:47 AM
[
{
"Date":"Tuesday December 1",
"Title":"OVERCOMING BY PRAYER",
"Memorise":"And when he was at the place, he said unto them, Pray that ye enter not into temptation.",
"Verse":"Luke 22:40",
"Read Chapter":"Luke 22:39-41",
"Read Text":"39 And he came out, and went, as he was wont, to the mount of Olives; and his disciples also followed him. \n40 And when he was at the place, he said unto them, Pray that ye enter not into temptation. \n41 And he was withdrawn from them about a stone's cast, and kneeled down, and prayed, ",
"Bible In One Year":"Psalm 57-63; Hebrews 6:13-7:10",
"Message":"The moment you gave your life to Jesus Christ, a battle line was drawn between you and the kingdom of darkness. Unhappy that he has lost you to God?s kingdom, Satan?s main goal is to secure your return to his kingdom. One major tool he tries to use in accomplishing this mission is temptation. The devil and his agents will bring temptations your way to make you to do things that would break your relationship with God. This is where prayer comes in. Prayer is one veritable tool that prevents you from falling into Satan?s traps. This is why Jesus urged His disciples at the Garden of Gethsemane to pray. Luke 22:40 says:\n?And when he was at the place, he said unto them, Pray that ye enter not into temptation.?\nWhen you pray, the strength to overcome temptations is released to you. A Christian who fails to pray will sooner or later become a victim of the tempter. The extent to which you will overcome temptations is determined by how robust your prayer life is. If you have been rising and falling so often, check your prayer life and the way you study God?s word. If you can improve on both, you will experience more stability in your faith.\n\nOne of the modus operandi of Satan is to show you dazzling sights that could tempt you to follow him. In other words, he presents an object of temptation in a bright and attractive way; repainting an evil thing in brilliant and attractive colours. For instance, a prostitute is labelled a commercial sex worker, in order to support the excuse that he or she is simply trying to earn a living, whereas the essence of prostitution is fornication and adultery. In tempting people, the devil and his agents make sin look attractive and inviting, almost with an air of irresistibility. Prayer deadens these illusions by painting sin in its real colours. Every time you pray over an object of temptation, it becomes less inviting and less attractive. As you pray, the desire to sin is brought under subjection. Through prayer, we obtain grace to walk in the reality of the truth that our flesh has been crucified (Galatians 5:24). Prayer strengthens our spirit man and keeps the flesh where it belongs: crucified and dead on the cross of Calvary (Romans 6:6). It connects your spirit with the Holy Spirit and separates you from the devil, by moving your focus towards Heaven and away from the allure of the world. How prayerful are you? No believer in Christ can triumph without using the weapon of prayer.",
"Notes":"Prayer Point:\nFather, please give me the grace to pray like Jesus prayed while on earth."
},
]
VulpesPosted Jan 16, 2015, 11:42 AM
Can you post the first few lines, please, so I can get an idea of how it's going to deserialize.
JOHN JOHNNNYPosted Jan 16, 2015, 11:27 AM
Let me tell you what i wanted to acheive. I am developing a daily devotional that has different devotion for each day. Instead of hard coding it i.e copy pasting for each day in the code, i want to store everything in json file in my project folder and refference it to appear in the textblock. Kiindly help this is my code without json reader or stream reader and how am i going to add json file into this code below
xaml
xaml
xaml.cs
protected override void OnNavigatedTo(NavigationEventArgs e)
{
AddDevotions();
int index = DateTime.Now.DayOfYear;
textblock.Text = devotions[index];
}
List
private void AddDevotions()
{
devotions.Add("4 Faith works miracles");
devotions.Add("5 Faith works miracles");
devotions.Add("6 Faith works miracles");
devotions.Add("7 Faith works miracles");
devotions.Add("8 Faith works miracles");
}
VulpesPosted Jan 16, 2015, 10:44 AM
The code suggests that you're trying to deserialize a JSON file to some kind of array but it's not then clear how this should be turned into a string.
Also you seem to be calling the ReadJsonFile method repeatedly with the same filepath in the AddDevotions method. So all the elements in your devotions list will be the same.