TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Calling Webservice in C# for JSON Data
Hemlata Kushwaha
Aug 11
2015
Code
1.2
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Net;
using
System.Text;
using
System.IO;
using
System.Web.Services;
using
System.Web.Script.Services;
using
System.Web.Script.Serialization;
using
Newtonsoft.Json.Linq;
public
partial
class
_Default : System.Web.UI.Page
{
protected
void
Page_Load(
object
sender, EventArgs e)
{
CallWebService();
}
private
void
CallWebService()
{
StringBuilder str =
new
StringBuilder();
string
url =
"Put your Service url"
;
WebRequest request = WebRequest.Create(url);
using
(WebResponse response = (HttpWebResponse)request.GetResponse())
{
using
(StreamReader reader =
new
StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
string
json = reader.ReadToEnd();
var serializer =
new
JavaScriptSerializer();
var objects = JArray.Parse(json);
str.Append(
"<table>"
);
foreach
(JObject root
in
objects)
{
string
id =
""
;
string
title =
""
;
string
date =
""
;
string
longdesc =
""
;
foreach
(KeyValuePair<String, JToken> app
in
root)
{
if
(app.Key ==
"id"
) id = app.Value.ToString();
if
(app.Key ==
"title"
) title = app.Value.ToString();
if
(app.Key ==
"date"
) date = app.Value.ToString();
if
(app.Key ==
"longdesc"
) longdesc = app.Value.ToString();
}
str.Append(
"<tr><td><h1>"
+ title +
"</h1></td></tr>"
);
str.Append(
"<tr><td>"
+ date +
"</td></tr>"
);
str.Append(
"<tr><td>"
+ longdesc +
"</td></tr>"
);
}
str.Append(
"</table>"
);
//---------Social Sharing
divShare.InnerHtml = str.ToString();
}
}
}
}
JSON data
Webservice in C#