Thanks Daniel for your response,
In my app, the user enters a name in japanese language which is to be
sent to server in a JSON request.The server expects the name parameter
in form of a UTF-8 encoded string.
So the encoding of source code should not matter.Here is the sample code
public static String EncodeJson(String strJson)
{
try
{
String strHex = null;
for (int i = 0; i < strJson.length(); i++)
{
try{
//char c = strJson.charAt(i);
// int j = (int) c;
// System.out.println("ASCII OF "+c +" = " + j + ".");
//System.out.println("strJson.charAt(i), 10=
"+Integer.parseInt(""+strJson.charAt(i), 10));
if ( strJson.charAt(i) >= 126)
{
String start = strJson.substring(0, i);
String end = strJson.substring(i + 1);
int jsonChar = strJson.charAt(i); // new
string(strJson[i], 1);
strHex = "000" + Integer.toHexString(jsonChar);
strHex = strHex.toUpperCase();
strJson = start + "\\u" +
strHex.substring(strHex.length()-4)+ end;
i += 5;
}
}catch(Exception e){
System.out.println(" Exception -
strJson.charAt(i)= "+strJson.charAt(i));
}
}
return strJson;
}
catch (Exception ex)
{
Log.i("Encode", ex.toString());
return strJson;
}
}
The output of this method is passed to JSON object using its put
method.At this point an additional "\" gets added to the parameter
due to which the server does not recognize this as a valid UTF string
Thanks,
Alok.
On Fri, Nov 12, 2010 at 9:07 PM, Daniel Drozdzewski
<[email protected]> wrote:
> On Fri, Nov 12, 2010 at 3:01 PM, Alok Kulkarni <[email protected]> wrote:
>> Hi,
>> I am having a unicode string "\u3403" which is actualy some japansee
>> character
>> I want to pass it through a JSON object. So i put the value as say
>> String str = "\u3403"
>> jsonObject.put("name",str);
>> When i do this the json object internally adds another escape
>> sequence as "\\u3403", and the request string has two "\" slashes.
>> This is interpreted wrongly by the server as it does not detect
>> unicode name.
>> What can i do for this ?
>> Thanks,
>> Alok.
>
> Alok,
>
> "\u3403" is not a unicode string, but a string that has hex code of an
> unicode character in it. It can be unicode encoded or its encoding can
> be anything else really.
> Since "\" is a special character and has extra meaning in Java String
> class, it gets escaped by escape character, which is "\". Now you
> know, what is the extra meaning of "\" ;-)
>
> Now JSON should not have any problems with parsing such
> character/string, even when double escaped. Make sure that your server
> is correctly configured for use of unicode.
> Also try sending the actual character (paste it from some place that
> generates a character based on its hex code), but please make sure
> that your source code is unicode encoded, otherwise it will not work.
>
> Daniel
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en