PHP: How to json encode of hindi language in response
Asked Answered
C

4

7

I am working with a translation API but here is an issue. I am using JSON in response and when I do json_encode of Hindi then the out is like "\u092f\u0939 \u0915\u093e\u0930 \u0939\u0948"

My code is given below

$data = array();
$data['hindi'] = 'यह कार है';
$data['english'] = 'This is car';
echo json_encode($data); die;

and the response is

{"hindi":"\u092f\u0939 \u0915\u093e\u0930 \u0939\u0948","english":"This is car"}
Cribbage answered 12/5, 2015 at 9:33 Comment(0)
L
5

This is correct json and when you display it in the browser and / or parse it, it will result in an object with the correct keys and values:

var json_string = '{"hindi":"\u092f\u0939 \u0915\u093e\u0930 \u0939\u0948","english":"This is car"}',
    json = JSON.parse(json_string);

// or directly:
var json2 = {"hindi":"\u092f\u0939 \u0915\u093e\u0930 \u0939\u0948","english":"This is car"};

console.log(json_string);
console.log(json);
console.log(json2);

document.write(json_string);
document.write('<br>');
document.write(json.hindi);
document.write('<br>');
document.write(json2.hindi);
Laceylach answered 12/5, 2015 at 9:49 Comment(0)
D
9

If you are running PHP 5.4 or greater, pass the JSON_UNESCAPED_UNICODEparameter when calling json_encode

Example:

$data = array();
$data['hindi'] = 'यह कार है';
$data['english'] = 'This is car';
echo json_encode($data, JSON_UNESCAPED_UNICODE);
die;
Domeniga answered 12/5, 2015 at 9:38 Comment(2)
Depending on where you want to use the JSON, however, not escaping the characters might cause problems. If he only uses it locally, simply json_decodeing the JSON again should give him back the original text.Predicament
OK but Now the response is not in JSON but it is showing string onlyCribbage
L
5

This is correct json and when you display it in the browser and / or parse it, it will result in an object with the correct keys and values:

var json_string = '{"hindi":"\u092f\u0939 \u0915\u093e\u0930 \u0939\u0948","english":"This is car"}',
    json = JSON.parse(json_string);

// or directly:
var json2 = {"hindi":"\u092f\u0939 \u0915\u093e\u0930 \u0939\u0948","english":"This is car"};

console.log(json_string);
console.log(json);
console.log(json2);

document.write(json_string);
document.write('<br>');
document.write(json.hindi);
document.write('<br>');
document.write(json2.hindi);
Laceylach answered 12/5, 2015 at 9:49 Comment(0)
P
0

The reason for this is likely that these characters are not in UTF-8 (I could not find them, at least). From the PHP documentation on json_encode:

All string data must be UTF-8 encoded.

This means that it will have to convert it to a 'description' of the characters. Do not worry, if you decode it again it will very likely be correct.

Predicament answered 12/5, 2015 at 9:38 Comment(0)
B
0

I have a solution to this problem. It works fine for me.

$data = array();
$data['hindi'] = base64_encode('यह कार है');
$data['english'] = 'This is car';
$encoded_text=json_encode($data);

To retrieve the hindi part of data:

$hindi=base64_decode(json_decode($encoded_text)->hindi);
Blackburn answered 23/1, 2018 at 16:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.