0

How can i read this json string using a loop, the intention is to create/append a table with 3 columns containing in each row one of this json values, not sure how can i achieve it

{
"1":
  {
    "update":"Ja existe",
    "numero":1,
    "registro":"IBRAM - 2014"
  },
"2":
  {
    "update":"Ja existe",
    "numero":2,
    "registro":"PM-SP - 2014"
  },
"3":
  {
    "update":"Ja existe",
    "numero":3,
    "registro":"ARTESP - 2014"
  }
}

3 Answers 3

1

If that is a string, then you can convert it to a JSON object and iterate through each element as follows:

var json = JSON.parse('THE JSON STRING');
for (key in json) {
  var value = json[key];
  // do whatever you wanna do with the value
}
Sign up to request clarification or add additional context in comments.

Comments

0

Parse the JSON string & then for loop for retrieve value. Try something like below

<script>
var data = '{"1":{"update":"Ja existe","numero":1,"registro":"IBRAM - 2014"},"2":{"update":"Ja existe","numero":2,"registro":"PM-SP - 2014"},"3":{"update":"Ja existe","numero":3,"registro":"ARTESP - 2014"}}';
var json = JSON.parse(data);
for (var key in json) {
    var value = json[key];
    console.log(value.update);
    console.log(value.numero);
    console.log(value.registro);
}
</script>

Comments

0

you can use for in and loop on it

var myjson = $.parseJSON('< the json string>');

    for (key in myjson ) {

        var myitem = myjson[key];
        alert(myitem.update);

    }

but if in your code above its not a json string its already serialize in json ready to be iterated. see this http://jsfiddle.net/725cev7s/

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in

2 Comments

Not sure how i can acess the elements inside the json, yes its a string
you need to parse it into a JSON object by using $.parseJSON() then you can iterate on it using for in

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.