2016-05-31 13 views
1

Ich möchte eine Datentabelle mit Name, Position, Telefon und E-Mail von der folgenden JSON generieren. Aber ich weiß jetzt nicht, wie ich auf den Namen und den verschachtelten Wert zugreifen soll. Das Ändern des JSON ist nicht möglich.jquery datatables verschachtelte Werte json

JSON:

{ 
    "key1" : "somevalue", 
    "key2" : "somevalue2", 
    "contacts" : { 
    "John Doe" : { 
     "position" : "CEO", 
     "phone" : "1234-5678-0", 
     "email" : "[email protected]" 
    }, 
    "Elvis Presley" : { 
     "position" : "Singer", 
     "phone" : "0234-5678-0", 
     "email" : "[email protected]" 
    }, 
    "Albert Einstein" : { 
     "position" : "Thinker", 
     "phone" : "0000-8888-0", 
     "email" : "[email protected]" 
    } 
} 

ERGEBNIS:

+-----------------+----------+-------------+---------------------+ 
| NAME   | POSITION | PHONE  | EMAIL    | 
+-----------------+----------+-------------+---------------------+ 
| John Doe  | CEO  | 1234-5678-0 | [email protected] | 
+-----------------+----------+-------------+---------------------+ 
| Elvis Presley | Singer | 0234-5678-0 | [email protected] | 
+-----------------+----------+-------------+---------------------+ 
| Albert Einstein | Thinker | 0000-8888-0 | [email protected] | 
+-----------------+----------+-------------+---------------------+ 
+0

Wenn IS JSON ist, ist es eine Zeichenfolge, wie parsen Sie es? Wenn es nur ein JavaScript-Objekt ist, dann ist das etwas anderes. Bitte klären Sie. –

Antwort

1

Sie können die Daten mit Hilfe ajax.dataSrc Option manipulieren.

Zum Beispiel:

var table = $('#example').DataTable({ 
    ajax: {  
     url: 'https://api.myjson.com/bins/4nnmy', 
     dataSrc: function(json){ 
      var data = []; 
      for(var contact_name in json.contacts){ 
      var contact = json.contacts[contact_name]; 
      data.push([ 
       contact_name, 
       contact['position'], 
       contact['phone'], 
       contact['email'] 
      ]);     
      } 

      return data; 
     } 
    } 
}); 

Siehe this jsFiddle für Code und Demonstration.

+0

perfekt, danke! – MDeuerlein

0

Wenn Sie die Variable mit dem Namen Daten haben, dann können Sie data.contacts zugreifen, und von dort aus können Sie das tun:

for(var contact in data.contacts) { 
    if(data.contacts.hasOwnProperty(contact)) { 
     // Do whatever with data.contacts[contact].position 
     // Do whatever with data.contacts[contact].phone 
     // Do whatever with data.contacts[contact].email 
    } 
} 
0

Beispiel: https://jsfiddle.net/9aLvd3uw/204/

Html:

<table> 
<thead> 
    <tr> 
    <th>NAME</th> 
    <th>POSITION</th> 
    <th>PHONE</th> 
    <th>EMAIL</th> 
    </tr> 
</thead> 
<tbody id="myTbody"> 

</tbody> 
</table> 

js:

var data = { 
    "key1" : "somevalue", 
    "key2" : "somevalue2", 
    "contacts" : { 
    "John Doe" : { 
     "position" : "CEO", 
     "phone" : "1234-5678-0", 
     "email" : "[email protected]" 
    }, 
    "Elvis Presley" : { 
     "position" : "Singer", 
     "phone" : "0234-5678-0", 
     "email" : "[email protected]" 
    }, 
    "Albert Einstein" : { 
     "position" : "Thinker", 
     "phone" : "0000-8888-0", 
     "email" : "[email protected]" 
    } 
}}; 
var contacts = data.contacts; 
var string = ''; 
for (name in contacts){ 
string += '<tr><td>' +name + '</td><td>' + contacts[name].position + '</td><td>' + contacts[name].phone + '</td><td>' + contacts[name].email + '</td></tr>' ; 
} 
$("#myTbody").html(string); 
+0

Vielen Dank, aber das ist keine Lösung für Jquery-Databases. siehe http://datatables.net – MDeuerlein