2012-03-24 26 views
2
http://www.taxmann.com/TaxmannWhatsnewService/Services.aspx?service=gettopstoriestabnews 

這是我的web服務,我必須分析和存儲String所有的價值,請幫助我如何解析。如何JSON解析在ASP .NET

使用asp.net(C#),這樣我可以存儲:

news_id as it variable 

news_title as title variable 

news_short_description as description 

news_date as date ; 

請幫助我,我在新的.NET我嘗試,但沒能做到

+1

*請*不使用**「PLZ」**,但**「請」**。 – digEmAll 2012-03-24 10:51:38

+2

我想更好的是「PLZ不使用'PLZ'但'請'':P – 2012-03-24 10:53:54

+0

也許這有幫助嗎? http://stackoverflow.com/questions/1212344/parse-json-in-c-sharp – Aristos 2012-03-24 10:58:43

回答

2

你可以使用這個類

public class News 
{ 
    public string news_id; 
    public string news_title; 
    public string news_short_description; 
    public string news_date; 
} 

反序列化響應字符串

using (var wc = new WebClient()) 
{ 
    JavaScriptSerializer serializer = new JavaScriptSerializer(); 
    var result = serializer.Deserialize<News[]>(wc.DownloadString("http://www.taxmann.com/TaxmannWhatsnewService/Services.aspx?service=gettopstoriestabnews")); 
    foreach (var item in result) 
    { 
     Console.WriteLine(item.news_title); 
    } 
} 
+0

比你非常我得到了奧特把我想要這個東西 – 2012-03-24 11:29:06

+0

@AnilKumar看到這個[鏈接](http://meta.stackexchange。 com/questions/5234/how-does-accepting-an-answer-work) – 2012-03-24 11:33:02

+0

可以將plz綁定數據,以便我們可以在列表視圖中顯示 – 2012-03-24 11:41:00

1

您還可以使用「DataContractJsonSerializer」,而不是JavaScriptSerializer,它使用WCF來(德)序列化JSON 閱讀更多信息組件:從herehttp://publicityson.blogspot.com/2010/06/datacontractjsonserializer-versus.html

+0

避免DCJS。當[de]序列化Enums,DateTimes,Anonymous Types併爲字典生成大於必需(不正確,IMO)的JSON時,它不那麼靈活。 – 2012-03-24 12:47:22

+0

是的,有缺點。但是JavaScriptSerializer不能正確處理遞歸(如果你有對象通過引用其他對象來引用它自己)。所以每個組件都有專業和缺點,這就是爲什麼我鏈接了文章。 – Fabske 2012-03-24 12:55:35

+0

@Fabske'你也可以使用「DataContractJsonSerializer」而不是JavaScriptSerializer'OP不使用任何東西。這不是一個答案,只是我的回答評論。我個人更喜歡[Json.Net](http://json.codeplex.com/),只要我有選擇的選項。 – 2012-03-24 20:01:16

1

首先下載一個JSON庫。並添加對您的項目的引用(右鍵單擊您的項目=>添加引用=> NewtonSoft.Json.dll)。之後,做一些編碼...

using System.Newtonsoft.Json; 
using System.Newtonsoft.Json.Linq; 
using System.Net; 

static void Main(string[] args) 
    { 
     WebClient c = new WebClient(); 
     var data = c.DownloadString("http://www.taxmann.com/TaxmannWhatsnewService/Services.aspx?service=gettopstoriestabnews"); 

     JObject o = JObject.Parse(data); 
     Response.Write("NewsID: "+ o["news_id"]); 
     Response.Write("NewsTitle: " + o["news_title"]); 


    }