user3673471
user3673471

Reputation: 1

Loading JSON file in Xamarin

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

Answers (2)

Narendra Sharma
Narendra Sharma

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

Shyju M
Shyju M

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

Related Questions