Grab Object in JSON

Asked

Viewed 486 times

1

How can I take only the property "name":"josimara" in that code ajax, I’m new in this area, thank you for your help.

<script src="http://www.habbid.com.br/assets/js/jquery-1.9.1.js" type="text/javascript"></script>

<script type="text/javascript">

var yql_url = 'https://query.yahooapis.com/v1/public/yql';

var url = 'https://www.habbo.com.br/api/public/users?name=josimara';

$.ajax({
  'url': yql_url,
  'data': {
    'q': 'SELECT * FROM json WHERE url="'+url+'"',
    'format': 'json',
    'jsonCompat': 'new',
  },
  'dataType': 'jsonp',
  'success': function(response) {
    console.log(response);
    document.getElementById("test").innerHTML = JSON.stringify(response);
  },
  'error': function(error) {
    document.getElementById("test").innerHTML = "error";
  }
});
</script>

<span id='test'>nothing</span>
  • you want to get the information from var url?

  • Our friend Sergio has already given the solution

  • This @rLinhares

1 answer

4


What you seek is

 document.getElementById("test").innerHTML = response.query.results.json.name;

If you look at the JSON that the API returns, these are the properties you have to go through.

Example:

var yql_url = 'https://query.yahooapis.com/v1/public/yql';
var url = 'https://www.habbo.com.br/api/public/users?name=josimara';

$.ajax({
  'url': yql_url,
  'data': {
    'q': 'SELECT * FROM json WHERE url="' + url + '"',
    'format': 'json',
    'jsonCompat': 'new',
  },
  'dataType': 'jsonp',
  'success': function(response) {
    console.log(response);
    document.getElementById("test").innerHTML = response.query.results.json.name;
  },
  'error': function(error) {
    document.getElementById("test").innerHTML = "error";
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://www.habbid.com.br/assets/js/jquery-1.9.1.js" type="text/javascript"></script>



<span id='test'>nothing</span>

  • Hello @Sergio a doubt because with this JSON giving undefined value http://shipdesk.in/mobile/mobile_quick.php?country=IN&to_country=IN&pin=560001&weight=2.5&unit=KG&need_cod=No&to_pin=700001 I want to catch the "Service"

  • @Josimara this json has an array inside. Then you have to go by numeric key.

  • could give a solution to us? Thanks

  • @Josimara yes, but I don’t know what you want to get out of this json, it’s different from the question. What is the key you want, and you only want one of them, or some?

  • wanted to take out the key "Service":"Delhivery_surface"

  • @Josimara all or only one?

  • Taking the value of this json object Url already kills the riddle {"content": {"url": "meusite.com.br","valor": "4.50"}}

  • @Josimara didn’t notice the last comment. That’s another json? this is identical to the question, ie without array.

  • I’ll set it straight so you see the final result, a moment.

  • Ready @Sergio {"query":{"Count":1,"created":"2017-10-03T18:37:07Z","lang":"en-BR","Results":{"content":{"url":"meusite.com.br","value":"4.50"}}}} would need to pick up the url, is another json

  • @Josimara var resultado = response.query.results.content;

  • in case it would be Document.getElementById("test"). innerHTML = Response.query.Results.json.content.url; ?

  • @Josimara I don’t know what JSON is talking about, but what you’re talking about in this comment would be: document.getElementById("test").innerHTML = response.query.results.content.url;

  • 1

    Solved, Hug!

Show 9 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.