How to Consume & get Proper Data of WCF return Json in J2ME
I have created a WCF Service and then after consuming this service in J2ME. My WCF Service is:
IService: [OperationContract] [WebInvoke(Method = "GET", UriTemplate = "/GetByCity/Limit={limit}", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] ItemList GetByCity(string limit); Service.CS : public ItemList GetByCity(string limit) { DataTable City = ds.Tables["City"]; var items = WithHTML(City, limit); return new ItemList { Items = items }; } public List<Item> WithHTML(DataTable City, string limit) { var items = (from d in City.AsEnumerable() where d.Field<string>("strMainHin") != string.Empty orderby d.Field<DateTime>("dtPosted") select new Item { ItemId = d.Field<Int32>("intId").ToString(), ItemLine = htmlEscapes(d.Field<string>("strMainHin")), Photo = @"http://192.168.1.17:801/ImageById/" + d.Field<Int32>("intId") + ".jpg" }).Take(Convert.ToInt32(limit)).ToList(); return items; } public string htmlEscape(string input) { var output = Regex.Replace(input, @"&#([0-9]*);", x => new String((char)int.Parse(x.Groups[1].Value), 1)); return output; }
Now the output of this service in the URL http://192.168.1.17:803/Patrika/Service.svc/GetByCity/Limit=2 is:
{"Items":[{"ItemLine":"डेढ़ करोड़ का क्रिकेट सट्टा पकड़ा","ItemId":"821745","Photo":"http:\/\/192.168.1.17:801\/ImageById\/821745.jpg"},{"ItemLine":"पार्किग का इंतजाम नहीं, तो जब्त होंगे वाहन","ItemId":"824837","Photo":"http:\/\/192.168.1.17:801\/ImageById\/824837.jpg"}]}
But when i Consume this service in J2ME through this link: 'http://192.168.1.17:803/Patrika/Service.svc/GetByCity/Limit=2' the Unicode or UTF-8 i am giving return responce in J2ME is:
{"ItemLine":"डेढ़ करोड़ का क�रिकेट सट�टा पकड़ा"}
Everything is going fine but only this string with some unicode is giving wrong output. then i tried to send only one data that is:
{"ItemLine":"डेढ़ करोड़ का क्रिकेट सट्टा पकड़ा"}
then at J2ME end i took this string into a JSON Object and put it into a label like following:
Label l1=new Label("ItemLine"); this.component.add(l1);
but the output is the same as above that bad json string.
Answers
IService:
[OperationContract] [WebInvoke(Method = "GET", UriTemplate = "/GetByCity/Limit={limit}", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] ItemList GetByCity(string limit);
Service.CS :
public ItemList GetByCity(string limit) { DataTable City = ds.Tables["City"]; var items = WithHTML(City, limit); return new ItemList { Items = items }; } public List<Item> WithHTML(DataTable City, string limit) { var items = (from d in City.AsEnumerable() where d.Field<string>("strMainHin") != string.Empty orderby d.Field<DateTime>("dtPosted") select new Item { ItemId = d.Field<Int32>("intId").ToString(), ItemLine = htmlEscapes(d.Field<string>("strMainHin")), Photo = @"http://192.168.1.17:801/ImageById/" + d.Field<Int32>("intId") + ".jpg" }).Take(Convert.ToInt32(limit)).ToList(); return items; } public string htmlEscape(string input) { var output = String.Join("", WebUtility.HtmlDecode(input).Select(x => "\\u" + ((int)x).ToString("X4"))); return output; }
In the Last method input is:
युवती का अधजला शव मिला
and the Output is:
\\\u0921\\\u0947\\\u0922\\\u093C \\\u0915\\\u0930\\\u094B\\\u0921\\\u093C \\\u0915\\\u093E \\\u0915\\\u094D\\\u0930\\\u093F\\\u0915\\\u0947\\\u091F \\\u0938\\\u091F\\\u094D\\\u091F\\\u093E \\\u092A\\\u0915\\\u0921\\\u093C\\\u093E
In this output I got extra '\' so this was not rendering in J2ME.
So at the Client end means at J2ME side I split the string which I got and then convert \\\ to \\ .
And that's all I got my Hindi Font: युवती का अधजला शव मिला.
Thanks to @Codo who helped me a lot.