Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read a local json file and display

newbie here,I could not find any example on Xamarin Forms read a local json file and display it. I need to do a local testing to read the local Json file.

1) Where do I save the json file for reading? in Android and iOS Projects or just in PCL project?

2) How to read the file?

here the code but it is not complete as I dont how to read the file.

using (var reader = new System.IO.StreamReader(stream))
{


var json = reader.ReadToEnd();

var rootobject = JsonConvert.DeserializeObject<Rootobject>(json);

whateverArray  = rootobject.Whatever;

}

The code miss the Path and others which required.

like image 739
MilkBottle Avatar asked Jun 16 '17 03:06

MilkBottle


People also ask

How do I read local JSON data?

One standard method we can use to read a JSON file (either a local file or one uploaded to a server) is with the Fetch API. It uses the same syntax for both. The only difference would be the URL.

How do I fetch and display JSON?

The jQuery code uses getJSON() method to fetch the data from the file's location using an AJAX HTTP GET request. It takes two arguments. One is the location of the JSON file and the other is the function containing the JSON data. The each() function is used to iterate through all the objects in the array.

How do I visualize a JSON file?

Use your JSON REST URL to visualize. Click on the Load URL button, Enter URL and Submit. Users can also visualize JSON in graph by uploading the JSON file. JSON Visualizer works well on Windows, MAC, Linux, Chrome, Firefox, Edge, and Safari.

How do I display a JSON object?

Declare a JSON object and store it into variable. Use JSON. stringify(obj) method to convert JavaScript objects into strings and display it.


1 Answers

You can directly add your JSON file in PCL. Then change build action to Embedded Resource

Now you can read Json data by:

    var assembly = typeof("<ContentPageName>").GetTypeInfo().Assembly;
        Stream stream = assembly.GetManifestResourceStream("Your_File.json");

    using (var reader = new System.IO.StreamReader(stream))
        {

            var json = reader.ReadToEnd();
            var data= JsonConvert.DeserializeObject<Model>(json);
        }
like image 107
Renjith Avatar answered Sep 25 '22 02:09

Renjith