Download Code

Xamarin JSON Parser Example Android

In this tutorial you will learn about JSON(JavaScript Object Notation) basic.How to parse data using Xamarin JSON Parser example android application.You can learn more about JSON from w3Schools.

Xamarin JSON

  • Stands for JavaScript Object Notation.
  • for storing and exchanging data.
  • lightweight independent language.
  • easier to use alternative of XML.

Syntax

  • Data in name/value pairs.
  • Data is separated by commas.
  • Curly braces hold objects.
  • Square brackets hold arrays.

xamarin JSON Parser example android, Xamarin JSON,Xamarin JSON Parser, Xamarin JSON Parser example, JSON parser example android, JSON parser example, Xamarin JSONObject, Xamarin JSONArray,

JSON Parser

JSON Parser is basically used to convert machine language to that language which can understood by app.

Create new android application project.Go to Main.axml file and a TextView.

xamarin JSON Parser example android, Xamarin JSON,Xamarin JSON Parser, Xamarin JSON Parser example, JSON parser example android, JSON parser example, Xamarin JSONObject, Xamarin JSONArray,

Next MainActivity.cs class.Do the following steps.

  • Create variable of TextView and refer it’s id.
  • Create variable to store extracted string.
  • Create variable and store JSONstring in it.
  • Under exception handling getParentJSONObject.
  • Then getJSONArray.
  • Store extracted string in a string .
  • Set extracted string to TextView.

MainActivity.cs

using Android.App;
using Android.Widget;
using Android.OS;
using Org.Json;

namespace XamarinJSON
{
	[Activity(Label = "XamarinJSON", MainLauncher = true, Icon = "@mipmap/icon")]
	public class MainActivity : Activity
	{
		
		TextView output;   // textview variable 
		string data = "";  // variable to display data

		//JSONString from which data extract
		string strJson = "{'Android Codec' :["
				+ "{'id':'01','name':'Expandable ListView','words':'500'},"
				+ "{'id':'02','name':'JSON Data','words':'1022'},"
				+ "{'id':'03', 'name':'Spinner','words':'510'}"
				+ "] "
				+ "}";


		protected override void OnCreate(Bundle savedInstanceState)
		{
			base.OnCreate(savedInstanceState);

			// Set our view from the "main" layout resource
			SetContentView(Resource.Layout.Main);
			initialize();
			try
			{
				//getting parent object from JSON String
				JSONObject parentObject = new JSONObject(strJson);

				//Get the instance of JSONArray that contains JSONObjects
				JSONArray jsonArray = parentObject.OptJSONArray("Android Codec");

				//displaying JSONArray data in textView
				for (int i = 0; i < jsonArray.Length(); i++) { JSONObject jsonObject = jsonArray.GetJSONObject(i); // getting JSON Object at I'th index //getting string of "name" => id and parse into integer
					int id = int.Parse(jsonObject.OptString("id"));
					//getting string of "name" => name
					string name = jsonObject.OptString("name");
					// getting string and parse into float
					float total_words = float.Parse(jsonObject.OptString("words"));
					// storing parsed data into string "data"
					data += "\n id= " + id + " \n Name= " + name + " \n Words= " + total_words + " \n ";
				}
				// setting data string to textview output
				output.Text = data;
			}
			catch (JSONException e) { 
				e.StackTrace.ToString();
			}

		}
		public void initialize()
		{
			output = (TextView)FindViewById(Resource.Id.textView1);
		}
	}
}

Run your android application project.

xamarin JSON Parser example android, Xamarin JSON,Xamarin JSON Parser, Xamarin JSON Parser example, JSON parser example android, JSON parser example, Xamarin JSONObject, Xamarin JSONArray,

That’s it for this tutorial.

For Xamarin JSON Parser Example Android like example in Java visit Android Codec.

Thanks.