2
http://www.taxmann.com/TaxmannWhatsnewService/Services.aspx?service=gettopstoriestabnews 

This is my web service I have to parse and store all value in String please help me how to parse.

using asp.net (C#) so that I can store :

news_id  as it variable

news_title as  title variable

news_short_description   as description

news_date as date ;

Please help me I'm new in .net I tried but not able to do

digEmAll
  • 56,430
  • 9
  • 115
  • 140
Anil Kumar
  • 77
  • 1
  • 3
  • 8

3 Answers3

2

First download a JSON library from here. And add reference to your project (Right click to your project=>Add reference=> NewtonSoft.Json.dll). After that do some coding...

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"]);


    }
Sagar
  • 7,115
  • 6
  • 21
  • 35
2

You can use this class

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

to deserialize the response string

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);
    }
}
L.B
  • 114,136
  • 19
  • 178
  • 224
  • Thanyou very much i got oout put i want this thing – Anil Kumar Mar 24 '12 at 11:29
  • can u plz bind the data so that we can display in listview – Anil Kumar Mar 24 '12 at 11:41
  • 2
    @AnilKumar You asked something and got your answer. You canceled your `accept` since I didn't answer your **second** question? Not nice. Don't be a [help vampire](http://slash7.com/2006/12/22/vampires/) – L.B Mar 24 '12 at 11:57
1

You can also use "DataContractJsonSerializer" instead of JavaScriptSerializer, it's the component used by WCF to (de)serialize JSON Read this for more info: http://publicityson.blogspot.com/2010/06/datacontractjsonserializer-versus.html

Fabske
  • 2,106
  • 18
  • 33
  • Avoid DCJS. It's less flexible when [de]serializing Enums, DateTimes, Anonymous Types, and produces larger than necessary (incorrect, IMO) JSON for Dictionaries. – Dave Ward Mar 24 '12 at 12:47
  • Yes there are drawback. But JavaScriptSerializer doesn't handle recursivity correctly (if you have object referencing itself via references to other objects). So each components have pro & cons, it's why I linked the article. – Fabske Mar 24 '12 at 12:55
  • @Fabske `You can also use "DataContractJsonSerializer" instead of JavaScriptSerializer` OP doesn't use anything. This is not an answer, just a comment to my answer. And I, personally, prefer [Json.Net](http://json.codeplex.com/) whenever I have the option to choose. – L.B Mar 24 '12 at 20:01