最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to extract info from json output - Stack Overflow

programmeradmin3浏览0评论

I have a json output that looks like this..

{
   "XXXX": {
      "name": "Dalvin Nieger",
      "work": [
         {
            "department": {
               "name": "Sales"
            },
            "start_date": "0000-00"
         }
      ],
      "link": ".php?id=XXXXX",
      "id": "XXXXXX"
   },
   "XXXXXXX": {
      "name": "Nick Mercer",
      "work": [
         {
            "department": {
               "name": "Marketing"
            },
            "start_date": "0000-00",
            "end_date": "0000-00"
         }
      ],
      "link": ".php?id=XXXXXX",
      "id": "XXXXXX"
   }
}

Where XXXX is the id no. of the employee. I want to loop through the data and get id no., name, department he works in and end date for each employee using javascript.

I appreciate any help.

Thanks.

I have a json output that looks like this..

{
   "XXXX": {
      "name": "Dalvin Nieger",
      "work": [
         {
            "department": {
               "name": "Sales"
            },
            "start_date": "0000-00"
         }
      ],
      "link": "http://www.my-site./profile.php?id=XXXXX",
      "id": "XXXXXX"
   },
   "XXXXXXX": {
      "name": "Nick Mercer",
      "work": [
         {
            "department": {
               "name": "Marketing"
            },
            "start_date": "0000-00",
            "end_date": "0000-00"
         }
      ],
      "link": "http://www.my-site./profile.php?id=XXXXXX",
      "id": "XXXXXX"
   }
}

Where XXXX is the id no. of the employee. I want to loop through the data and get id no., name, department he works in and end date for each employee using javascript.

I appreciate any help.

Thanks.

Share Improve this question edited Jul 25, 2010 at 9:21 Nick asked Jul 25, 2010 at 9:06 NickNick 3854 gold badges8 silver badges16 bronze badges 3
  • Your JSON is invalid. You appear to have the body of an object (key: value pairs) inside the body of an array. You need to fix that before you can do anything sensible with the data. – Quentin Commented Jul 25, 2010 at 9:11
  • 2 What's with massive downvoting, people? It could be just a typo created when obfuscating the private data - go easy on the guy. – kander Commented Jul 25, 2010 at 9:16
  • @david sorry my bad. I guess I didn't hold down the shift key long enough. – Nick Commented Jul 25, 2010 at 9:24
Add a ment  | 

3 Answers 3

Reset to default 5

Your JSON isn't correct - it's wrapped in an array but you don't use keys in JSON arrays. If you changed the outer brackets ([) to braces ({) you'd just be able to loop through the JSON object keys in JavaScript. See here for instructions.

I'm not sure I pletely understand the question, but would this do what you want, assuming you'll fix the problem with your JSON as pointed out?

for(i in jsonData) {
  console.log("id is " + i);
  console.log("name is " + jsonData[i].name);
  console.log("department is " + jsonData[i].work[0].department.name);
  console.log("enddate is " + jsonData[i].work[0].end_date);
}

once you fix the json it should look like this and work like this

var jsonData = {
   "XXXX": {
      "name": "Dalvin Nieger",
      "work": {
            "department": {
               "name": "Sales"
            },
            "start_date": "0000-00"
         },
      "link": "http://www.my-site./profile.php?id=XXXXX",
      "id": "XXXXXX"
   },
   "XXXXXXX": {
      "name": "Nick Mercer",
      "work": {
            "department": {
               "name": "Marketing"
            },
            "start_date": "0000-00",
            "end_date": "0000-00"
         },
      "link": "http://www.my-site./profile.php?id=XXXXXX",
      "id": "XXXXXX"
   }
}
$.each(jsonData,function(id,data){
  var content = '<p>ID : '+id;
  if(typeof data == 'object')
    $.each(data,function(index,value){
        
     if(typeof value == 'object' && index=='work'){
       content += '<br/>'+index+' : '+value.department.name;
      }else
        content += '<br/>'+index+' : '+value;       
    });
  content += '</p>';
  $('#result').append(content);
   
})
<script src="https://ajax.googleapis./ajax/libs/jquery/2.0.2/jquery.min.js"></script>

<div id="result">
  </div>

发布评论

评论列表(0)

  1. 暂无评论