-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?

Ebrahim
  • 1,740
  • 2
  • 25
  • 31

1 Answers1

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/

I wrestled a bear once.
  • 22,983
  • 19
  • 69
  • 116
  • 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 Oct 05 '15 at 22:13
  • @Ebrahim - Ok, I answered your question, check my edit. If this helps please hit the check mark. – I wrestled a bear once. Oct 05 '15 at 22:31
  • 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! – Ebrahim Oct 05 '15 at 22:40
  • answer has nothing to do with generating an angular view with `ng-repeat` – charlietfl Oct 05 '15 at 22:47
  • @Ebrahim - I've given you the answer AND done the work for you. Please hit the checkmark. – I wrestled a bear once. Oct 05 '15 at 22:47
  • @Pamblam thanks, but is not the correct answer, you know , it is need the real answer, you want fake rankings? – Ebrahim Oct 05 '15 at 22:49
  • @Pamblam suggest you read the whole question before giving OP a hard time. Clearly states trying to push this into an angular view. I do agree however the data source structure does need clarification – charlietfl Oct 05 '15 at 22:59
  • @Pamblam you charge as much as you want, please don't waste your and mine also others time! – Ebrahim Oct 05 '15 at 23:01
  • @Ebrahim, I've now given you the answer three times. All you have to do is use the function i posted to flatten out your object (but change `[]` to `{}`) and use the resulting object in your `ng-repeat`. read the docs: https://docs.angularjs.org/api/ng/directive/ngRepeat – I wrestled a bear once. Oct 05 '15 at 23:07
  • @Pamblam thanks for your all assist, but It is not the answer, maybe I should say the problem like this: I have a nested object, I want each object to be an element of the array first of all, and then easily iterate through that array like using ng-repeat or what else, my problem here is that they are only objects, and I can't traverse, I want they to be firstly elements in array, then each element is an object. do you need more clarification? I sent a hangout invitation to your gmail – Ebrahim Oct 05 '15 at 23:22
  • I don't use gmail, I have a secure protonmail account :P can you maybe make a jsfiddle but just write the expected output as HTML and forget about the JavaScript part. Just let me see what you're trying to do and maybe I can help you get there. @Ebrahim – I wrestled a bear once. Oct 05 '15 at 23:28
  • @Pamblam please look at this file https://www.dropbox.com/s/1anji6t3cnnfj7b/Screenshot%202015-10-06%2003.06.01.jpg?dl=0 here the folder names which I receive the JSON as the server side is shown here, but they are not sorted, I need better iteration and traverse through them, and also here is a simple JSfiddle http://jsfiddle.net/ebrahimmm/ncykb0qq/5/ – Ebrahim Oct 05 '15 at 23:41
  • @Ebrahim - So, you want to iterate through just the inner paths from the JSON? - and you want to sort them? Sort by what? Alphabetically? – I wrestled a bear once. Oct 06 '15 at 00:12
  • It's a little difficult to tell exactly what kind of output you're looking for with that screenshot of code but I will give you this advice: You're clearly trying to parse the data and the output at the same time. First get your data model into an easily traverable model, then loop through it and build your HTML. Again, if you can throw together some HTML and forget about the javascript part for now, maybe I can see what you're trying to do. @Ebrahim – I wrestled a bear once. Oct 06 '15 at 00:18
  • wait i think you're trying to build like a breadcrubms type of thing.. i think i get it now – I wrestled a bear once. Oct 06 '15 at 00:20
  • @Ebrahim do yourself a big favor and standardize the property names at source within the objects within arrays ... will make it trivial to put into view – charlietfl Oct 06 '15 at 00:40