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:

&#2351;&#2369;&#2357;&#2340;&#2368; &#2325;&#2366; &#2309;&#2343;&#2332;&#2354;&#2366; &#2358;&#2357; &#2350;&#2367;&#2354;&#2366;

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.


Need Your Help

VC++ OLE DB: how to cancel query?

sql-server visual-c++ oledb

Sometimes user run long-time query to SQL Server in application (vc++ OLE DB).

Is it required that I use forge.request.ajax in stead of $.post?

trigger.io

First , I am not trying to do a cross site request.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.