0

Can someone please assist me in extracting the Sring from translatedText

{
    "data":{
        "translations":[
            {
                "translatedText":"நான் Google மொழிபெயர்ப்பாளர் இருந்து JSON பதில் சோதனை"
            }
        ]
    }
}

My Code:

<head>
<script>
var xmlHttp = null;

function GetCustomerInfo()
{
    var CustomerNumber = document.getElementById( "sourceText" ).value;
    alert(CustomerNumber);
    var myKey = 'ttes';
    var Url = "https://www.googleapis.com/language/translate/v2?key="+ myKey + "&source=en&target=ta&q=" + CustomerNumber + "&prettyprint=true"

    xmlHttp = new XMLHttpRequest(); 
    xmlHttp.onreadystatechange = ProcessRequest;
    xmlHttp.open( "GET", Url, true );
    xmlHttp.send( null );
}

function ProcessRequest() 
{
    if ( xmlHttp.readyState == 4 && xmlHttp.status == 200 || xmlHttp.status == 304) 
    {
        if ( xmlHttp.responseText == "Not found" ) 
        {
            document.getElementById( "TextBoxCustomerName"    ).value = "Not found";
            document.getElementById( "TextBoxCustomerAddress" ).value = "";
        }
        else
        {
            var info = eval ( '(' + xmlHttp.responseText + ')' );

            // No parsing necessary with JSON!        
            document.getElementById( "translatedText" ).innerHTML = info.data[0].translations[0].translatedText;
        }                    
    }
}
</script>
</head>
<body>
<input id="sourceText"/>
<button onclick="GetCustomerInfo()">Translate Me</button>
<br>
<div id="translatedText">
</div>
</body>

But translatedText is empty always!

5
  • @dystroy I have a div named translatedText Commented Feb 3, 2014 at 13:27
  • And with that edit, you've rendered all the answers obsolete. Consider posting all the relevant information right away, and don't put the solutions below into your question. Commented Feb 3, 2014 at 13:39
  • @cookiemonster Sorry, I was trying one of the answers and posted..! Commented Feb 3, 2014 at 13:39
  • Consider some basic debugging steps, like logging the various values to the developer console, and checking for errors. Commented Feb 3, 2014 at 13:41
  • Two things: 1) you don't need the [0] after info.data... check my response below 2) make sure you dont get this: {"error":{"errors":[{"domain":"usageLimits","reason":"keyInvalid","message":"BadRequest"}],"code":400,"message":"BadRequest"}} Commented Feb 3, 2014 at 13:46

4 Answers 4

2
var info = JSON.parse(responseText);    
var translation = info.data.translations[0].translatedText
Sign up to request clarification or add additional context in comments.

Comments

1
JSON.parse('{ "data": { "translations": [ { "translatedText": "நான் Google மொழிபெயர்ப்பாளர் இருந்து JSON பதில் சோதனை" } ] } }')['data']['translations'][0]['translatedText'];

or

JSON.parse('{ "data": { "translations": [ { "translatedText": "நான் Google மொழிபெயர்ப்பாளர் இருந்து JSON பதில் சோதனை" } ] } }').data.translations[0].translatedText;

2 Comments

I get the text, but unfortunately, I dont get it the exact language!
Worked if I send the JSON variable as such! Instead of the String..
0

try this

var info = eval ( '(' + xmlHttp.responseText + ')' );

Yeah, and also

info.data.translations[0]...

insread of

info.data[0].translations[0]...

3 Comments

Don't use eval. eval is Evil. use JSON.parse instead
Mindlessly parroting the "eval is evil" mantra isn't useful. This answer correctly shows the two issues in the code. +1 Though I'd personally prefer JSON.parse in this situation.
It worked, my mistake was accessing data[0] as you pointed
0

Try JSON.parse:

var myObject = JSON.parse(myJSONtext);

then u can use myObject to retrieve object properties.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.