您的位置:首页 > Web前端 > JavaScript

C# Call Web API and Parse JSON

2012-07-01 08:46 405 查看
Example:(Json file)

{
"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;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: