3

I have been trying to use Google Transliterate API using the RESTful approach as its easy to do so through server side language (C# here).

So, I came across this URL format: http://www.google.com/transliterate/indic?tlqt=1&langpair=en|hi&text=bharat%2Cindia&tl_app=3 which returns the JSON in the format:

[
{
"ew" : "bharat",
"hws" : [
"भारत","भरत","भरात","भारात","बहरत",
]
},
{
"ew" : "india",
"hws" : [
"इंडिया","इन्डिया","इण्डिया","ईन्डिया","इनडिया",
]
},
] 

I tried HttpWebRequest and HttpWebResponse to get the JSON but it returned values in Unicode on the web browser, such as:

[ { "ew" : "bharat", "hws" : [ "\u092D\u093E\u0930\u0924","\u092D\u0930\u0924","\u092D\u0930\u093E\u0924","\u092D\u093E\u0930\u093E\u0924","\u092C\u0939\u0930\u0924", ] }, { "ew" : "india", "hws" : [ "\u0907\u0902\u0921\u093F\u092F\u093E","\u0907\u0928\u094D\u0921\u093F\u092F\u093E","\u0907\u0923\u094D\u0921\u093F\u092F\u093E","\u0908\u0928\u094D\u0921\u093F\u092F\u093E","\u0907\u0928\u0921\u093F\u092F\u093E", ] }, ]

So, I applied this article and passed the JSON string via it, and it returned:

[ { "ew" : "bharat", "hws" : [ "भारत","भरत","भरात","भारात","बहरत", ] }, { "ew" : "india", "hws" : [ "इंडिया","इन्डिया","इण्डिया","ईन्डिया","इनडिया", ] }, ]

FIRST QUESTION: Am I doing it right so far? Because in the browser it DOES NOT show the last " ] ", however " ] " exists in the HTML source (not sure why that happened). Also, when I try to parse it, using (I might be wrong using this technique):

var jss = new JavaScriptSerializer();
var dict = jss.Deserialize<Dictionary<string, dynamic>>(the_JSON_string);

Its giving me error saying:

Invalid array passed in, extra trailing ','.

SECOND QUESTION: If I am doing right so far, can I get some help parsing the Hindi words? What approach should I take using preferably System.Web.Script.Serialization;. Eventually I want to grab the Hindi text for further processing.

Please help, thanks.

Community
  • 1
  • 1
Dev Dreamer
  • 289
  • 1
  • 9
  • 18

2 Answers2

3

I would recommend Json.Net to parse json strings. Below code(with your sample string) works and you don't need to do anything to unescape those characters. Json parsers will handle it for you.

string json = @"[ { ""ew"" : ""bharat"", ""hws"" : [ ""\u092D\u093E\u0930\u0924"",""\u092D\u0930\u0924"",""\u092D\u0930\u093E\u0924"",""\u092D\u093E\u0930\u093E\u0924"",""\u092C\u0939\u0930\u0924"", ] }, { ""ew"" : ""india"", ""hws"" : [ ""\u0907\u0902\u0921\u093F\u092F\u093E"",""\u0907\u0928\u094D\u0921\u093F\u092F\u093E"",""\u0907\u0923\u094D\u0921\u093F\u092F\u093E"",""\u0908\u0928\u094D\u0921\u093F\u092F\u093E"",""\u0907\u0928\u0921\u093F\u092F\u093E"", ] }, ]";

dynamic obj = JsonConvert.DeserializeObject(json);
MessageBox.Show(obj[0].hws[0].ToString());
L.B
  • 114,136
  • 19
  • 178
  • 224
  • thanks a lot. It totally works. Is there a way I can get to know the size of obj ? obj.Length does not work. Because the number of parameters could be many, so I might want to loop through the same. Thanks. – Dev Dreamer Aug 08 '12 at 10:45
  • @DevDreamer You can write `var length = obj.Count;` or `foreach (var item in obj) { MessageBox.Show(item.hws[0].ToString()); }` – L.B Aug 08 '12 at 10:48
  • can you please help me with this? http://stackoverflow.com/questions/12095188/parsing-the-json-with-multilingual-text-using-system-web-script-serialization-or Thanks. – Dev Dreamer Aug 23 '12 at 15:58
0

i think you can remove last comma like a below way

  the_JSON_string =  the_JSON_string.Remove(the_JSON_string.LastIndexOf(','));
sejal patel
  • 262
  • 2
  • 4
  • 12