简体   繁体   中英

Creating Array Names from JSON

I am parsing JSON and trying to create an array of arrays .

JSON

{"data": {

"intro":{
    "EN": "Hi. My name is Barry.<br/>I design things in Tokyo.",
    "JP": "Intro - こんにちは。私はバリーです。<br>東京のデジタルプロダクトデザイナー"
},

"cta":{
    "EN": "See my work",
    "JP": "CTA - 私のプロジェクト"
},

"aboutme":{
    "EN": "Test in nulla enim. Phasellus molestie magna non est venenatis nisl tempor. Suspendisse dictum feugiat nisl ut dapibus. Mauris iaculis porttitor posuere. Praesent id metus massa, ut blandit odio. Proin quis tortor orci. Etiam at risus et justo dignissim congue.</p> <p>Donec congue lacinia dui, a porttitor lectus condimentum laoreet. Nunc eu ullamcorper orci. Quisque eget odio ac lectus vestibulum faucibus eget in metus. In pellentesque faucibus vestibulum. Nulla at nulla justo, eget luctus tortor. Nulla facilisi. Duis aliquet egestas purus in blandit. Curabitur vulputate, ligula lacinia scelerisque tempor, lacus lacus ornare ante, ac egestas est urna sit amet arcu. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Sed molestie augue sit amet leo ",
    "JP": "About me - こんにちは。私はバリーです。<br>東京のデジタルプロダクトデザイナー"
}, 

"projects":{

    "project1": { 
        "titleEN": "Rams Razor",
        "titleJP": "ラムズライゾル",
        "EN": "Rams Razor is... Test in nulla enim. Phasellus molestie magna non est venenatis nisl tempor.Test in nulla enim. Phasellus molestie magna non est venenatis nisl tempor.Test in nulla enim. Phasellus molestie magna non est venenatis nisl tempor.",
        "JP": "Rams Razor is... こんにちは。私はバリーです。<br>東京のデジタルプロダクトデザイナー",
        "media": { 
            "media1": {
                "path":"img/rams-app.mp4",
                "background":"#292929",
                "type":"video"
            },
            "media2": { 
                "path":"img/rams-icon.png",
                "background":"#292929",
                "type":"img"
            },
            "media3": { 
                "path":"rams-double.png",
                "background":"#292929",
                "type":"img"
            },
            "media4": { 
                "path":"img/watch1.png",
                "background":"#989dae",
                "type":"img"
            }
        }
    },


    "project2": { 
        "titleEN": "Tallywag",
        "titleJP": "ラムズライゾル",
        "EN": "Tallywag is... Test in nulla enim. Phasellus molestie magna non est venenatis nisl tempor.Test in nulla enim. Phasellus molestie magna non est venenatis nisl tempor.Test in nulla enim. Phasellus molestie magna non est venenatis nisl tempor.",
        "JP": "Tallywag is... こんにちは。私はバリーです。<br>東京のデジタルプロダクトデザイナー",
        "media": { 
            "media1": {
                "path":"img/tallywag1.png",
                "background":"#292929",
                "type":"img"
            },
            "media2": { 
                "path":"img/tallywag1.png",
                "background":"#292929",
                "type":"img"
            },
            "media3": { 
                "path":"img/tallywag1.png",
                "background":"#292929",
                "type":"img"
            },
            "media4": { 
                "path":"img/tallywag1.png",
                "background":"#989dae",
                "type":"img"
            }
        }
    }


}


}}

JS

  $.getJSON("data/enjp.json", function(data) {

    data = data.data;

    //create project data arrays
    $.each(data.projects, function(i,val){

        projects.push(i); 
        console.log(projects);

    });

});

Everything is working great except when I push the array names to the PROJECTS array they are in quotes and my other scripts fall apart. The console outputs:

["project1", "project2"] 

Does anyone know how I can push them without quotes at all?

Thanks kindly.

If you want to put arrays into the projects then you need to push the arrays and not their names.

I can't see your data, but presumably that means val not i .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM