1

I am getting json from asana that is an object (data) of several objects. How do I make data an array?

{"data":{"id":5571422294129,"created_at":"2013-05-24T15:31:50.340Z","modified_at":"2013-05-24T15:32:21.260Z","name":"testProject","notes":"","archived":false,"workspace":{"id":5571305742112,"name":"TestITAT"},"followers":[{"id":5571289325327,"name":"John Doe"}]}}

I am trying to put this in a data table using aoColumns. If there is no need to convert "data" to an array please let me know how to use this JSON in datatables without it.

4
  • Do you understand the difference between an object and an array? It doesn't seem like it. Commented May 29, 2013 at 19:29
  • please explain what I'm missing. Commented May 29, 2013 at 19:35
  • Objects have keys that correlate to each entry. Arrays do not. They are unequivocal. There is no magical formula for translating them into an array. Commented May 29, 2013 at 19:38
  • @user2402595 It looks like you are missing everything. First, read this en.wikipedia.org/wiki/JSON , section "Data types, syntax and example". Then come back and explain what you want to have in your array. Commented May 29, 2013 at 19:43

2 Answers 2

1

It is not that complicated. You can use DataTables aaData for this. I assume your JSON contains multiple "data":{..}, "data":{..}, "data":{..} ?

Then, consider this as test data :

var data = [
{"data":{"id":1571422294129,"created_at":"2010-05-24T15:31:50.340Z","modified_at":"2010-05-24T15:32:21.260Z","name":"testProject","notes":"","archived":false,"workspace":{"id":5571305742112,"name":"TestITAT"},"followers":[{"id":5571289325327,"name":"John Doe"}]}},
{"data":{"id":2571422294129,"created_at":"2011-05-24T15:31:50.340Z","modified_at":"2011-05-24T15:32:21.260Z","name":"Project A","notes":"","archived":false,"workspace":{"id":5571305742112,"name":"TestITAT"},"followers":[{"id":5571289325327,"name":"John Doe"}]}},
{"data":{"id":3571422294129,"created_at":"2012-05-24T15:31:50.340Z","modified_at":"2012-05-24T15:32:21.260Z","name":"Project B","notes":"bla bla","archived":false,"workspace":{"id":5571305742112,"name":"TestITAT"},"followers":[{"id":5571289325327,"name":"John Doe"}]}}
];

HTML markup

 <table id="test">
   <thead>
     <tr>
        <th>archived</th>
        <th>created_at</th>
        <th>id</th>
        <th>modified_at</th>
        <th>name</th>
        <th>notes</th>
      </tr>
   </thead>
   <tbody>
   </tbody>
 </table>

convert JSON to aaData-array :

var aaData = [];
for (var i=0;i<data.length;i++) {
    aaData.push([ 
        data[i].data.archived,
        data[i].data.created_at,
        data[i].data.id,
        data[i].data.modified_at,
        data[i].data.name,
        data[i].data.notes
    ]);
}

Initialize the table

$('#test').dataTable({
   "aaData": aaData
});

result : enter image description here

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

Comments

0

I am not sure what you are exactly attempting to do, but...

If you are trying to deserialize the JSON into a data table you want something like this. This will take your JSON and deserialize it to an object, it won't exactly work with a data table but rather custom classes decorated with DataContract and DataMember attributes. But I think it may be a good starting point for you.

Public static T DeSerialize<T>(string strJSON)
{
      T obj = Activator.CreateInstance<T>(); 
      MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(strJSON));
      DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
      obj = (T)serializer.ReadObject(ms);

      ms.Close();
      ms.Dispose();
      return (obj);
}

Here is a very useful article regarding serializing JSON. HTH :)

http://pietschsoft.com/post/2008/02/NET-35-JSON-Serialization-using-the-DataContractJsonSerializer.aspx

Regards

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.