Learn Online JSON Parsing in Swift

JSON is very useful method for storing data. Learn Online JSON parsing in Swift. Get complete details from below:-

What is JSON?

JSON stands for JavaScript Object Notation. This chapter explains how to parse the JSON file and extract necessary information from it. JSON is one of the best method for storing data. In this Blog ,we are going to show you how to parse a JSON response from a URL and display it in a layout.

JSON is made up of three parts:

  • Key/Value Pairs separated by colons (:):   “Name”:”John”. “Name” is KEY, “John” is VALUE.
  • JSON Arrays: JSON Array is collection of JSON Objects and it is represented by square brackets [ ].
  • JSON Objects:  JSON Object is  represented by curly brackets { }.

Some example of json parsing

Example 1

[              // Square brackets declare a json array

{           // Curly brackets declare a json Object

       id: 11036,

       name: “The Supper Club”,

       address: “240 W 47th St, New York, NY”,

       country: “United States”

 },

{

    id: 7034,

    name: “XVI”,

    address: “251 W. 48th St, New York, NY”,

    country: “United States”

  }

]

 Description

  • first define “[” means we need to fetch array so create object of array  JSONArray jsonArray = new JSONArray(response); where response is our complete json code fetch from server, see in below demo.
  • Now Array contain more than one object so we need loop to fetch each object. for (int i = 0; i < ja.length(); i++) {…}
  • Second ” { ” bracket we fetch this object using this method getJSONObject(). JSONObject jo = ja.getJSONObject(i);
  • Retrive jason object’s each field using key

long id = jo.getLong(“id”);

String name = jo.getString(“name”);

String address = jo.getString(“address”);

String country = jo.getString(“country”);

where “id” is key and value is “11036”.
id is long value so fetch using getLong().
name is string so fetch using getString().

Example 2

{

“user”: [

{

“id”: “001”,

“name”: “Raj Amal”,

“email”: “raj.amalw@gmail.com”

},

{

“id”: “001”,

“name”: “Raj Amal”,

“email”: “raj.amalw@gmail.com”

}

]

}

Description

  • Here first define object and contain array of name user. so access object using syntax JSONObject json = new JSONArray(response).
  • Array of “user” contain two oject,Array fetch JSONArray jarray = jsono.getJSONArray(“user”);
  • Fetch first object JSONObject c = jarray.getJSONObject(0);
  • Fetch json field of first object

String id = c.getString(“id”);

String name = c.getString(“name”);

String email = c.getString(“email”);

  • Fetch Second object JSONObject c = user.getJSONObject(1);
  • Fetch json field of first object

String id = c.getString(“id”);

String name = c.getString(“name”);

String email = c.getString(“email”);

  • Or use for loop for access more than one object

for(int i=0;i<jarray.length();i++){

JSONObject c = jarray.getJSONObject(i);

String id = c.getString(“id”);

String name = c.getString(“name”);

String email = c.getString(“email”);

}


How it is work ?

  • We will send first request from MainActivity.java to InformationParsing.java class using parsing.getData() method.

                  c11

 

  • We will send an HttpRequest.java from InformationParsing.java class to HttpRequest.java file to fetch the JSON data from the server. Server will send the JSON data back to InformationParsing.java

c22

  • Parse the data which fetch from server.

c3

  • We kind of store that temporarily in our ParseModel.java class by creating new objects for each information.

c4

  • We create an CustomAdapter.java class to pass this data to the ListView.

c5

Step 1: Need Online-Json code  – url  :  http://microblogging.wingnity.com/JSONParsingTutorial/jsonActors

Step 2:  go on eclipse -> file -> new -> Android application project -> give application name

Step 3: add Internet permission in your AndroidManifest.xml file  <uses-permission android:name=“android.permission.INTERNET”/> 


Android Onlin Json Parsing demo : Download https://github.com/elluminati/Online-Json-parsing.git