This is just a quick addendum to a colleague's post about using JSON in .NET.

Original post

See Dan's post here: Google Translate and .NET (Dan Matthews)

Complete example code

This example translates the phrase "a piece of pie" into French using Google Translate.  I do not use any processing on the JSON output except to convert it into an XML document that can be processed in .NET:

using System;
using System.Net;
using System.Web;
using System.IO;
using System.Xml;
using System.Runtime.Serialization.Json;

namespace JSON_Example
{
    class Program
    {
        static void Main(string[] args)
        {
            WebRequest req = WebRequest.Create(
                "http://ajax.googleapis.com/ajax/" +
                "services/language/translate?v=1.0&q=" + 
                HttpUtility.UrlEncode("a piece of pie") + 
                "&langpair=" + "en" + "%7C" + "fr");
            WebResponse res = req.GetResponse();


            XmlDocument doc = new XmlDocument();
            using (Stream s = res.GetResponseStream())
            {
                XmlDictionaryReader xr = 
                    JsonReaderWriterFactory.CreateJsonReader(
                        s,
                        XmlDictionaryReaderQuotas.Max);

                doc.Load(xr);
                xr.Close();
                s.Close();
            }
            Console.WriteLine(doc.OuterXml);

            Console.ReadKey();
        }
    }
}

Required assemblies

  • System.Runtime.Serialization (3.0)
  • System.ServiceModel.Web (3.5)
  • System.Web (2.0)
  • System.Xml (2.0)
  • and the other usual suspects

Versions

  • Microsoft .NET Framework 3.5

Metadata


Bookmark with :
Digg It! DZone StumbleUpon Technorati Reddit Del.icio.us Newsvine Furl Blinklist
posted @ Thursday, May 22, 2008 4:05 PM | in .NET Software Development

Comments

Gravatar
# re: .NET: JSON support in WCF
Posted by rashmi
on 7/22/2008 8:58 AM
hi ,

with google translator we have converted english to Español..html pages are working fine..

http://www.discareuropa.com/

but if we use server side control..inside page are translating but it is refreshing its own .. if we press esc button from keyboard then it is stopping..

you can see the same by putting user name: admin password: admin
you will notices that page is redirected to lighterBrand.aspx ..can u help me how can i stop refreshing the inside page..rashmi
Gravatar
# re: .NET: JSON support in WCF
on 7/23/2008 2:54 PM
Hi Rashmi,

Looks like you haven't done this correctly. The JSON support is a service interface for getting translations, it does not provide translated web pages directly.

You need to translate the text and then display it as usual. It is not a good idea to do this for every request as it will massively slow down your web server and the user experience.

Thanks for your feedback.

Post Comment

Title *
Name *
Email
Url
Comment *  


Please add 2 and 4 and type the answer here: