2016-04-06 6 views
0

アム反復のJSONデータ応答</p> <p>これは私が<code>projects</code>値を取得する必要があり、この応答データobtained.from ..ですjosn応答からキー値を取得しようとして

{ '$': 
    { id: 'TP', 

    href: '/app/rest/projects/id:yuioTP', 
    webUrl: 'http://teamcity.jffjf 
' }, 
    parentProject: [ { '$': [Object] } ], 
    buildTypes: [ { '$': [Object] } ], 
    templates: [ { '$': [Object], buildType: [Object] } ], 
    parameters: [ { '$': [Object], property: [Object] } ], 
    vcsRoots: [ { '$': [Object] } ], 
    projects: [ { '$': [Object], project: [Object] } ] } 

コード

var parseString = require('xml2js').parseString; 
var async = require('async'); 

var getJson = function(callback) { 
    http.get(options, function(res) { 
     var data = ''; 
     res.setEncoding('utf8'); 
     res.on('data', function(chunk) { 
      data += chunk.toString(); 
     }); 
     res.on('end', function() { 
      var output = {}; 
      var err = null; 
      try { 

       parseString(data, function(err, result) { 
        async.each(result, function(test, test_callback) { 
         console.log(test.projects) 

        }, function(err) { 
         callback(null, report); 
        }); 
       }); 
      } catch (e) { 
       err = e; 
      } 
      // callback(err, output); 
     }) 
    }).on('error', function(err_) { 

     console.log(err_, url); 
     callback({ 
      error: err_ 
     }); 
    }); 
} 

getJson(); 


console.log(test.projects) 

//プリント....私は重要なプロジェクト内のオブジェクトのデータを取得できますか

[ { '$': { count: '9' }, 
    project: 
    [ [Object], 
     [Object], 
     [Object], 
     [Object], 
     [Object], 
     [Object], 
     [Object], 
     [Object], 
     [Object] ] } ] 
+0

あなたは 'Object.keys'を試みたことがありますか? –

+0

私はそれを使用する方法を知っていません..i didnmtは – Psl

答えて

1

Object.keysメソッドを使用します。下の簡単な例。あなたは、私はそれはあなたにfiddleを助けることを願っています<object>.projectsを反復処理し、Object.keys方法

<object>.projects.forEach(function(item){ 
    Object.keys(item).forEach(function(key){ 
    console.log(key + ' : ' + item[key]) 
    }) 
} 

に各項目を渡す必要がありますあなたの場合は

var a = { 
    foo: "hello", 
    baz: "world" 
} 

var array = [a,a,a,a,a] 

array.forEach(function(item){ 
    Object.keys(item).forEach(function(key){ 
    console.log(key + ' : ' + item[key]) 
    }) 
}) 

+0

を試しましたプロジェクトを後退させます:[オブジェクトオブジェクト]のみ – Psl

0

これを試してみてください:

projects[0].project.forEach(function(item){ 

    console.log(item[0]); //log whole object 
    var temp = item[0]; 
    Object.keys(temp).forEach(function(key){ 
    console.log(key + ' : ' + temp[key]); //log value by key 
    }); 
}); 
関連する問題