Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

JavaScript Object Notation (JSON)

Processing

Janaka Balasooriya, Ph.D.


Lecturer, Barrett Honors Faculty
Arizona State University
Objectives

JSON Basics JSON Processing


Explain ideas and Analyze JSON data
elementary and process JSON
structures in the data using C#
JSON data and .NET constructs.
representation.
What is JSON?

| JSON is a simple, light-weight


data exchange format often
used in web applications
including REST web API calls.
{
| Though it is called JavaScript “first_name”: “Janaka”
Object Notation, JSON is “last_name”: “Balasooriya”
language- and platform- “address”: {
independent. “street”: “ mill ave”
“city”: “tempe”
“zipcode”: 85281
}
phone: [“121-324”, “122-341”]
}
What is JSON?

| JSON can be processed by


almost all the browsers and all
commonly used programming
languages for web and
distributed computing. {
“first_name”: “Janaka”
| JSON uses text-based “last_name”: “Balasooriya”
structure to represent data
using the key-value pair “address”: {
structure. “street”: “ mill ave”
“city”: “tempe”
| The text-based structure
facilitates easy data exchange “zipcode”: 85281
between servers and clients. }
phone: [“121-324”, “122-341”]
}
Basic JSON Structures

JSON Definitions Key: value


| Data is represented as key/value { }
pairs. [ ]
| Curly braces represent objects. ,

| Square brackets represent arrays.


{
| Multiple values are separate by “first_name” : “Janaka”
comma. “last_name”: “Balasooriya”
| Data Types Supported by JSON: “address” : {
- a string “street”: “ mill ave”
- a number
- another JSON object “city” : “tempe”
- an array “zipcode”: 85281
- a boolean }
- null phone : [“121-324”, “122-341”]
}
Processing API JSON Result
| Different languages and frameworks provide various ways
to process JSON results from a web API call.
| Both .NET framework and C# use two techniques in
processing JSON:
- Use the JavaScriptSerializer defined in
System.Web.Script.Serialization library
- Use JsonConvert defined in the Newtonsoft.Json library
| With either approach, the first step is to understand the
JSON object structure and develop the code to process
the JSON structure.
Processing API JSON Result

Objects

{
“first_name”: “Janaka”
“last_name”: “Balasooriya”
“address”: {
“street”: “ mill ave”
“city”: “tempe”
“zipcode”: 85281
}
phone: [“121-324”, “122-341”]
}

String array
Processing API JSON Result C# Code
string url =
@"http://localhost:22815/Service1.svc/setNameAge?
f_name=Janaka&l_name=Bala";
HttpWebRequest request =
(HttpWebRequest)WebRequest.Create(url);
WebResponse response = request.GetResponse();
Stream responseStream = response.GetResponseStream();

StreamReader reader = new StreamReader(responseStream);


String json = reader.ReadToEnd();

personInfo p =
JsonConvert.DeserializeObject<personInfo>(json);

Console.WriteLine(p.first_name);
Console.WriteLine(p.address.city);
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; static void processJsonJavaScriptSerializer(String
using System.Text; data)
using System.Net.Http; {
using System.Net; JavaScriptSerializer js = new
using System.Text; JavaScriptSerializer();
using System.IO; personInfo p = js.Deserialize<personInfo>(data);
using Newtonsoft.Json; Console.WriteLine(p.first_name);
using System.Web.Script.Serialization;
namespace Client }
{
class Program // Beginning of the program
{
static void Main(string[] args)
{
string url = @"http://localhost:22815/Service1.svc/setNameAge?
f_name=Janaka&l_name=Bala";

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);


WebResponse response = request.GetResponse();
Stream responseStream = response.GetResponseStream();

StreamReader reader = new StreamReader(responseStream);


String json = reader.ReadToEnd();

processJsonJavaScriptSerializer(json);
processJsonNewtonSoft(json);
}
}
static void processJsonNewtonSoft(String data)
{
personInfo p =
JsonConvert.DeserializeObject<personInfo>(data);
Console.WriteLine(p.address.city);
}
Complete Code
[Serializable]
Objects
public class personInfo {
{ “first_name” : “Janaka”
public string first_name { get; set;
}
“last_name”: “Balasooriya”
public string last_name { get; “address” : {
set; } “street”: “ mill ave”
public Address address { get; set; } “city” : “tempe”
public string[] phone { get; set; }
}
“zipcode”: 85281
}
public class Address phone : [“121-324”, “122-341”]
{ }
public string street { get; set; }
public string city { get; set; } String array
public int zipcode { get; set; }
}

} // End of the program


Summary

| JavaScript Object Notation (JSON) uses several simple


constructs to exchange data in Web API calls:
- JSON structure elements: Objects, Arrays, Key:Value pair
elements.
- By combining these structural elements, data can be
organized into more hierarchical composite data objects.
| Due to its simplicity and light-weight data processing
requirements, JSON has become the most popular data
exchange format in the REST web API world.
| Various platforms and programming languages provide
JSON processing libraries.
- .NET supports JSON processing based on
JavaScriptSerializer or NewtonSoft third party library.

You might also like