Reputation: 1
I am using the following code to load JSON file in Xamarin. The JSON file is a simple file containing name and age of one person.
using (StreamReader r = new StreamReader("First_json.json"))
{
string json = r.ReadToEnd();
P1 = JsonConvert.DeserializeObject<Person>(json);
//MessageBox.Show(P1.name);
}
The code was working correctly in c# but in Xamarin while using StreamReader
the error pops up saying unable to convert string to stream argument. I have searched the internet to find a better way to read the file but I haven't succeeded so far.
Upvotes: 0
Views: 3010
Reputation: 682
Provide your First_json file with extension as .json "First_json.json".
Your class should be like this:
public class Person
{
public int Age { get; set; }
public string Name { get; set; }
}
Now it will work. Let me know your result.
Upvotes: 0
Reputation: 9923
Try this,
Add the JSON file into the Asset folder of your project
sample : {"Age":30,"Name":"testuser"}
and read like this
using (var reader = new StreamReader(Assets.Open("First_json.txt")))
{
var jsonData = reader.ReadToEnd();
var questionsList = JsonConvert.DeserializeObject<Person>(jsonData);
}
Upvotes: 1