.NET: JSON support in WCF

by Stephen Horsfield 22. May 2008 15:05

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

Tags:

Software Development

Comments

7/22/2008 7:58:59 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

rashmi |

7/23/2008 1:54:35 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.

Stephen Horsfield |

Powered by BlogEngine.NET 1.5.0.7
Theme by Interakting

Interakting

A full service digital agency offering online strategy, design and usability, systems integration and online marketing services that deliver real business benefits and ensure your online objectives are met.

Calendar

<<  July 2010  >>
MoTuWeThFrSaSu
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

View posts in large calendar