-3

Hi I get a JSON response like this:

{
"20150917":
 {
     "Daily01sec":
        {
            "TG01152600000": "\/20150917\/Daily01sec\/TG0115260.bin",
            "TG01152600600": "\/20150917\/Daily01sec\/TG0115260.bin"
        }
    },
"201510":
    {
        "05":
        {
            "Daily01sec":
            {
                "TG01152780600": "\/201510\/05\/Daily01sec\/TG01152780600.bin"                       
            }
        }
    },
"201509":
    {
        "05":
        {
            "Daily01sec":
            {
                "TG01152780600": "\/201510\/05\/Daily01sec\/TG01152780600.bin"                     
            }
        }
    }
}

I want an array of indexes of and its value to the corresponding data.

I want to have data["20150917"],data["201510"],data["201509"] and the corresponding information for it.

Actually the nested data would be an array of nested data in it,

Can be parsed with angularJS ng-repeat? any idea?

8
  • my target is to convert the first nested objects into array of objects Commented Oct 5, 2015 at 21:55
  • 1
    var object = JSON.parse(json_text); BTW your json_text seems to be not consistent. Commented Oct 5, 2015 at 21:56
  • @MaxZoom my code seems parsed JSON, also if I do that, it also says SyntaxError: Unexpected token o at Object.parse (native) Commented Oct 5, 2015 at 21:58
  • @MaxZoom I can't get what you mean by the last elements! Commented Oct 5, 2015 at 22:02
  • @MaxZoom I removed those last commas Commented Oct 5, 2015 at 22:10

1 Answer 1

1

In JavaScript an object is an associative array.

In other words myobj.data is the same thing as myobj['data'] - the two can be used interchangeably. So all you have to do is parse your JSON (if it's not already) and you're good to go.

data = JSON.parse(json); 
console.log(data['20150917']);

http://jsfiddle.net/byjawop8/

EDIT

You're looking for for..in syntax, which looks like this:

for(index in json){
    document.getElementById('r').insertAdjacentHTML('beforeEnd', index+" " + json[index]['05']['Daily01sec']['TG01152780600'] + "<br>");
}

http://jsfiddle.net/yjedwLws/1/

EDIT AGAIN

I gave you what you needed in my last edit, but I'll give you one more hint. This function iterates through the JSON, and as long as it's formatted in the way you posted above, this will convert the whole thing into an array you can easily iterate through.

function arrayConvert(json){
    var arr = [];
    for(index in json){
        var obj = json[index];
        var key = null;
        while(typeof obj == 'object'){
            for(ind in obj){
                if(key == null) key = ind;
                obj = obj[ind];
            }
        }
        arr.push({'key':key, 'val':obj});
    }
    return arr;
}

And another fiddle: http://jsfiddle.net/yjedwLws/2/

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

16 Comments

Thanks for your response, I don't know the index "20150917", because it may change in another JSON I get from the server! hence I need iterating thorough them easily, this is the problem you get me?
@Ebrahim - Ok, I answered your question, check my edit. If this helps please hit the check mark.
thanks, I need to iterate in various JSON responses like this. so I don't know the structure at first, your answer is well, when I know the case and is not dynamically changing!
answer has nothing to do with generating an angular view with ng-repeat
@Ebrahim - I've given you the answer AND done the work for you. Please hit the checkmark.
|

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.