Skip to content

Instantly share code, notes, and snippets.

@MBehtemam
Last active December 19, 2015 03:39
Show Gist options
  • Save MBehtemam/5891912 to your computer and use it in GitHub Desktop.
Save MBehtemam/5891912 to your computer and use it in GitHub Desktop.
Reading Book publishers with OpenLibray API
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using System.Net;
using System.IO;
using System.Runtime.Serialization.Json;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Web.Script.Serialization;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
private readonly static string OpenLibraryUrl =
"http://openlibrary.org/api/books?bibkeys=ISBN:{0}&jscmd=data&format=json";
protected void Button1_Click(object sender, EventArgs e)
{
string formattedUri = String.Format(OpenLibraryUrl, TextBox1.Text);
WebRequest request = WebRequest.Create(formattedUri);
WebResponse response = request.GetResponse();
// HttpWebRequest webRequest = GetWebRequest(formattedUri);
// HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
string jsonResponse = string.Empty;
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
jsonResponse = sr.ReadToEnd();
//List<string> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<string>>(sr.Read().ToString());
//Response.Write(jsonResponse);
//foreach (string item in list)
//{
// a += item;
//}
//dynamic json = JsonConvert.DeserializeObject(jsonResponse);
//Response.Write(json.publishers);
JObject o = JObject.Parse(jsonResponse);
Response.Write("<hr/>");
Response.Write(o["ISBN:" + TextBox1.Text + ""]["publishers"][0]["name"]);
}
//System.Web.Script.Serialization.JavaScriptSerializer jsSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
//BookInfo books = jsSerializer.Deserialize<BookInfo>(jsonResponse);
//DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(BookInfo));
//BookInfo books = (BookInfo)jsonSerializer.ReadObject(response.GetResponseStream());
//object o= Newtonsoft.Json.JsonConvert.DeserializeObject(jsonResponse);
}
private static HttpWebRequest GetWebRequest(string formattedUri)
{
// Create the request’s URI.
Uri serviceUri = new Uri(formattedUri, UriKind.Absolute);
// Return the HttpWebRequest.
return (HttpWebRequest)System.Net.WebRequest.Create(serviceUri);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment