Hide JSON element using javascript

Asked

Viewed 688 times

1

I have the following result in JSON:

 {
  "name": "test",
  "count": 5,
  "frequency": "Manual Crawl",
  "version": 1,
  "newdata": true,
  "lastrunstatus": "success",
  "thisversionstatus": "success",
  "thisversionrun": "Wed Jun 24 2015 16:25:51 GMT+0000 (UTC)",
  "results": {
    "collection1": [
      {
        "property1": "",
        "property2": "DIA DA TERRA",
        "index": 1,
        "url": "http://site.com.br"
      },

I need to show only the property2 element, how to hide the other elements (property1, index, url) using javascript?

  • 2

    How do you hide? Do not show? That the server only sends part? You can explain better?

  • @Sergio this JSON result shows the following on the screen: EARTH DAY index 1 url http://www.site.com.br, when you should only show EARTH DAY.

  • I just got back to the computer, have you solved the problem? I went to the site but I couldn’t find what you refer.

  • @Sergio hasn’t figured it out yet...

  • Try to use: Return data[0].results.colletion1.property2; but it did not work. In case it was to show only property2 Element

  • put a little more of your code, so it might be easier to help

Show 1 more comment

3 answers

1

and only use as an example object:

var json = {
  "name": "test",
  "count": 5,
  "frequency": "Manual Crawl",
  "version": 1,
  "newdata": true,
  "lastrunstatus": "success",
  "thisversionstatus": "success",
  "thisversionrun": "Wed Jun 24 2015 16:25:51 GMT+0000 (UTC)",
  "results": {
    "collection1": [
        
      {
        "property1": "",
        "property2": "DIA DA TERRA",
        "index": 1,
        "url": "http://site.com.br"
      },
      {
        "property1": "",
        "property2": "DIA DA TERRA 2",
        "index": 1,
        "url": "http://site.com.br"
      }
    ]
    
  }
}

alert(json.results.collection1[0].property2);
alert(json.results.collection1[1].property2);

only access it by object. JSFIDDLE

  • Gabriel, would that be so? var results = ' { "Results": { "property2":"EARTH DAY" }, }'; var result = JSON.parse(results); Alert(result.Results);

  • Alert(json.results.collection1[0].property2); and you can scroll through the indices [0],[1],[2] until the Property information runs out

  • It didn’t work. I’ll test it here. Obg.

  • http://jsfiddle.net/o6azw0u2/1/

  • At least where I am programming (Kimonolabs) the following error: "error": "Bad Request", "message": "Your Function failed to evaluate.". It is worth remembering that the server already starts javascript with the following function: Function Transform(data) ? Return data; }

  • I tried to use: Return data.results.collection1[0]. property2; . It returns NULL.

  • runs the above example.

Show 2 more comments

0

Gustavocave, your case JSONcomes as a parameter of an ajax function (eg: success, done, etc), consider literally OMITTING the keys propery1, index and url.

Example

$.ajax( "recuperaJSON.php" )
.done(function(resultado) {

    // EXIBIR SOMENTE PROPRIEDADE "property2"
    // para cada resultado
    $.each(data['results'], function(key, value) 
    {
        $.each(data['results'][key], function(key_result, value_result)
        {
            // considere haver um elemento HTML com id "idResultado", por exemplo 
            $('#idResultado').append(value_result['property2'] + "<br>");
        });    
    });

});

Demonstration

See your JSON saved in a variable called json and then iteration between the results, displaying only "propery2":

https://jsfiddle.net/felipe_douradinho/a8y48k4m/

0

Guys, I got it using the following:

function transform(data) { 
    data.results.collection1.map(function(item) {  
       delete item.index;  
       return item;  
    }); 
    return data; 
};

Browser other questions tagged

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