C# Call Web API and Parse JSON
2012-07-01 08:46
405 查看
Example:(Json file)
Solution:
############################################################################################################################
Project for UCD
{ "displayFieldName" : "OBJECT_NAME", "fieldAliases" : { "OBJECT_NAME" : "OBJECT_NAME", "OBJECT_TYPE" : "OBJECT_TYPE" }, "positionType" : "point", "reference" : { "id" : 1111 }, "objects" : [ { "attributes" : { "OBJECT_NAME" : "test name", "OBJECT_TYPE" : "test type" }, "position" : { "x" : 5, "y" : 7 } } ] }
Solution:
using System.Collections.Generic; using System.Web.Script.Serialization; public class NameTypePair { public string OBJECT_NAME { get; set; } public string OBJECT_TYPE { get; set; } } public enum PositionType { none, point } public class Ref { public int id { get; set; } } public class SubObject { public NameTypePair attributes { get; set; } public Position position { get; set; } } public class Position { public int x { get; set; } public int y { get; set; } } public class Foo { public Foo() { objects = new List<SubObject>(); } public string displayFieldName { get; set; } public NameTypePair fieldAliases { get; set; } public PositionType positionType { get; set; } public Ref reference { get; set; } public List<SubObject> objects { get; set; } } static class Program { const string json = @"{ ""displayFieldName"" : ""OBJECT_NAME"", ""fieldAliases"" : { ""OBJECT_NAME"" : ""OBJECT_NAME"", ""OBJECT_TYPE"" : ""OBJECT_TYPE"" }, ""positionType"" : ""point"", ""reference"" : { ""id"" : 1111 }, ""objects"" : [ { ""attributes"" : { ""OBJECT_NAME"" : ""test name"", ""OBJECT_TYPE"" : ""test type"" }, ""position"" : { ""x"" : 5, ""y"" : 7 } } ] }"; static void Main() { JavaScriptSerializer ser = new JavaScriptSerializer(); Foo foo = ser.Deserialize<Foo>(json); } }
############################################################################################################################
Project for UCD
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 Newtonsoft.Json; namespace SearchEngineApplication { public class RESULT { [System.Runtime.Serialization.DataMember] public int c { get; set; } [System.Runtime.Serialization.DataMember] public string display_url { get; set; } [System.Runtime.Serialization.DataMember] public string main_slashtag_boosted { get; set; } [System.Runtime.Serialization.DataMember] public int n_group { get; set; } [System.Runtime.Serialization.DataMember] public string short_host { get; set; } [System.Runtime.Serialization.DataMember] public string short_host_url { get; set; } [System.Runtime.Serialization.DataMember] public string snippet { get; set; } [System.Runtime.Serialization.DataMember] public string url { get; set; } [System.Runtime.Serialization.DataMember] public string url_title { get; set; } } public class DYM { public int yes { get; set; } } [System.Runtime.Serialization.DataContract] public class blekko { [System.Runtime.Serialization.DataMember] public DYM DYM { get; set; } [System.Runtime.Serialization.DataMember] public List<RESULT> RESULT { get; set; } [System.Runtime.Serialization.DataMember] public string THEMENUM { get; set; } } public partial class blekkoAPI : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void CallAPI_Click(object sender, EventArgs e) { var json1 = new WebClient().DownloadString("http://blekko.com/ws/?q=cure+for+headaches+/json+/ps=1&auth=f4c8acf3&p=1"); string json = JsonConvert.SerializeObject(json1); blekko deserializedProduct = JsonConvert.DeserializeObject<blekko>(json1); Label1.Text = "Display_Url:" + deserializedProduct.RESULT.First().display_url + " </br> " + "main_slashtag_boosted:" + deserializedProduct.RESULT.First().main_slashtag_boosted + "</br> " + "Description:" + deserializedProduct.RESULT.First().snippet; } } }
相关文章推荐
- Use C# to get JSON Data from the Web and Map it to .NET Class => Made Easy! 转
- C# web api 返回类型设置为json的两种方法
- C# web api返回类型设置为json的两种方法
- Using C# 4.0 and dynamic to parse JSON
- C#中关于搜狗地图web服务api的httpresponse中回应json中文乱码的问题 Encoding
- C# web api返回类型设置为json的两种方法
- C#.NET WebApi返回各种类型(图片/json数据/字符串),.net图片转二进制流或byte
- C# web api返回类型设置为json的两种方法
- C# web api返回类型设置为json的两种方法
- C# web api返回类型设置为json的两种方法
- C#WebApi通过Get方法存Json到数据库
- 【C#MVC】 使用HttpWebRequest访问外部API,并返回json信息 (Access_Key)
- Web and php parse json data
- C# WebApi 返回JSON
- C# web api 对象与JSON互转
- C# WebAPI with HttpWebRequest, HttpWebResponse and HttpContext Example
- C# web api返回类型设置为json的两种方法
- C#WebAPI返回json去掉双引号前面的反斜杠
- C# web api返回类型设置为json的两种方法
- Craw the data of the web page and parse to pdf