2

I want to loop through my json response. My json response looks like this

{"line":[{"type":"bank","name":"ABN","account":"NL47ABNA0442660960","description":"Bijgewerkt t\/m 30-10-2014","balance":"6.266,55","image":""},{"type":"bank","name":"Rabo","account":"NL89RABO0177896647","description":"","balance":"0,00","image":""}],"total":"6.266,55"}

What I want is a foreach loop through all lines so i get the keys and the values for every line.

6
  • you don't loop through json. you decode the json to a native structure (e.g. array/object) and loop through that. Commented Nov 13, 2014 at 14:11
  • Take a look at stackoverflow.com/questions/8785660/… Commented Nov 13, 2014 at 14:12
  • question isn't very clear, do want the keys by known name or do you want to output unknown key names for each object? Commented Nov 13, 2014 at 14:15
  • @Marc B - JSON stands for JavaScript Object Notation, so you don't need to decode it to loop through it. If it is encoded - i.e. is a string with JSON format - than you need to decode the string. But in his code, it is already a JSON object. Commented Nov 13, 2014 at 14:22
  • @Markai: yes, but json doesn't get automatically decoded into a javascript array/object/whatever automatically, UNLESS, e.g. you tell jquery you're expecting json back as a response from an ajax request. Until it's actually decoded, it's just a string. Commented Nov 13, 2014 at 14:23

5 Answers 5

6

You could iterate like this: (added code-comments for explanation)

var result = document.getElementById("result");
var json = '{"line":[{"type":"bank","name":"ABN","account":"NL47ABNA0442660960","description":"Bijgewerkt t\/m 30-10-2014","balance":"6.266,55","image":""},{"type":"bank","name":"Rabo","account":"NL89RABO0177896647","description":"","balance":"0,00","image":""}],"total":"6.266,55"}';

var obj = JSON.parse(json);
// json object contains two properties: "line" and "total".
// iterate "line" property (which is an array but that can be iterated)
for (var key in obj.line) {
    // key here is the index of line array
    result.innerHTML += "<br/>" + key + ": ";
    // each element of line array is an object
    // so we can iterate over its properties
    for (var prop in obj.line[key]) {
        // prop here is the property
        // obj.line[key][prop] is the value at this index and for this prop
        result.innerHTML += "<br/>" + prop + " = " + obj.line[key][prop];
    }
}
// "total" is a property on the root object
result.innerHTML += "<br/><br/>Total = " + obj.total;
<p id="result"> </p>

Demo Fiddle: http://jsfiddle.net/abhitalks/ajgrLj0h/2/

.

Sign up to request clarification or add additional context in comments.

Comments

1
var json = {"line":[{"type":"bank","name":"ABN","account":"NL47ABNA0442660960","description":"Bijgewerkt t\/m 30-10-2014","balance":"6.266,55","image":""},{"type":"bank","name":"Rabo","account":"NL89RABO0177896647","description":"","balance":"0,00","image":""}],"total":"6.266,55"}; 
   for(var i = 0; i < json.line.length; i++)
   {
       console.log("Type: " + json.line[i].type + " Name: " + json.line[i].name + " Account: " + json.line[i].account + " Description: " + json.line[i].description + " Balance: " + json.line[i].balance + " Image: " + json.line[i].image); 
   }

You can do something like that...

1 Comment

Thnx this is what I needed!
1
var json = {"line":[{"type":"bank","name":"ABN","account":"NL47ABNA0442660960","description":"Bijgewerkt t\/m 30-10-2014","balance":"6.266,55","image":""},{"type":"bank","name":"Rabo","account":"NL89RABO0177896647","description":"","balance":"0,00","image":""}],"total":"6.266,55"};

if(json.line !== undefined && json.line.length > 0){
    var key,value;
    json.line.map(function(lineObject){        
        for (key in lineObject) {
            value = (lineObject[key] == '')?'unknown': lineObject[key];
            console.log(key+":"+ value);
        }
        console.log("---------------------");
    });
}

http://jsfiddle.net/ddw7nx91/

Comments

0
var obj = {"line":[]} //your json here

for(var i=0; i<obj.line.length; i++) {
console.log(obj.line[i].type)
}

obj.line is an array, so you can get his length an cycle it.

Comments

0

This would create an array of lines each with a keys object and a values object.

var response = JSON.parse( {'your':'JSON'} );
var lines = [];

$.each( response, function( line ) {//loop through lines in response
    var keys = [];
    var values = [];
    $.each( line, function( obj ) {
        keys.push( Object.keys(obj) );//get keys
        for( var key in obj ) {
            values.push(obj[key]);//get values
        }
    });
    lines.push({ {'keys':keys},{'values':values} });
});

Comments

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.