0

I'm writing a mobile app to handle with some data from a database.

So my question is what is the best way to display the received data? I get the data as a json. I read a bit about knockout...

My problem is i dont know how much and what kind data i get. That should happen dynamically. The data could be something about personnel or information about an article.

I hope u understand my question ^^ Sry for my bad english.

3
  • There is no "best" way. When you parse the json string and create a javascript object, you need to display the object's property values on the page. Do you know how to parse the JSON string and retrieve the object's properties using javascript or jQuery? Commented Jul 8, 2013 at 14:01
  • Thanks for your fast comment. I know how to parse and retrieve the JSON Object. Actually i dont know how to display the received data in a list. Commented Jul 8, 2013 at 14:13
  • How to approach this would depend on whether the object has nested objects. For example, does the Article object have an Authors object, which contains multiple authors, or simply a single Authors property which contains a comma-delimited string of author names? Commented Jul 8, 2013 at 14:58

2 Answers 2

1

If you just want to display the data in a user-readable way, then iterate through the json object building an HTML UL from the data.

function display(obj, result) {
    if(obj == null)
        return result;
    var ul, li;
    for(var k in obj) {
        var value = obj[k];
        if(typeof(value) == "object") {
            li = result.appendChild(document.createElement("li"));
            li.innerHTML = k + ":";
            if(value != null) {
                ul = li.appendChild(document.createElement("ul"));
                display(value, ul);
            }
        } else {
            li = result.appendChild(document.createElement("li"));
            li.innerHTML = k + ": " + value;
        }
    }
    return result;
}
document.getElementById("displayDiv").appendChild(
    display(jsonObj, document.createElement("ul"));
);
Sign up to request clarification or add additional context in comments.

Comments

1

All deppends from your imagination

1) Create youe own custom dynamic list;

2) Use 3rd party one YUI http://developer.yahoo.com/yui/treeview/

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.