Access an element of a JSON dynamically

1

I have the following JSON:

    [{
    "posts": {
        "ID": 452,
        "post_date": "01\/01\/2016 15:30:00",
        "post_title": "Titulo do post"
    },
    "postmeta": {
        "anexo": {
            "size": {
                "value": "615",
                "unit": "KB"
            },
            "extension": "PDF"
        }
    },
    "taxonomies": {
        "tax_teste": {
            "term_id": 15,
            "name": "minha taxonomia",
            "slug": "minha-taxonomia"
        }
    }
}, {
    "posts": {
        "ID": 34,
        "post_date": "28\/11\/2015 10:13:22",
        "post_title": "Titulo do post2"
    },
    "postmeta": {
        "anexo": {
            "size": {
                "value": "39",
                "unit": "KB"
            },
            "extension": "JPG"
        }
    },
    "taxonomies": {
        "tax_teste": {
            "term_id": 132,
            "name": "taxonomia 2",
            "slug": "taxonomia-2"
        },
        "tax_local": {
            "term_id": 52,
            "name": "Rio de Janeiro",
            "slug": "rio-de-janeiro"
        }
    }
}]

You have an HTML example:

html = '<table class="tb-widget-materiais">';
html += '<tr class="item_widget item_widget_left">';
html += '<td class="left">';
html += '<a href="{LINK_MATERIAL}" target="_blank" title="{TITLE_MATERIAL}">';
html += '<img width="{WIDTH_IMG}" height="{HEIGHT_IMG}" src="{SRC_IMG}" class="attachment-eiWidgets size-eiWidgets wp-post-image" alt="{TITLE_MATERIAL}" title="{TITLE_MATERIAL}" />';
html += '</a>';
html += '</td>';
html += '<td class="right">';
html += '<p>{DATA_MATERIAL}</p>';
html += '<h3>';
html += '<a href="{LINK_MATERIAL}" target="_blank" title="{TITLE_MATERIAL}">';
html += '{TITLE_MATERIAL} - {SIZE_MATERIAL}</a>';
html += '</h3>';
html += '</td>';
html += '</tr>';
html += '<tr>';
html += '<td>';
html += '<a href="{SLUG_TAXONOMY}">{TAXONOMY}</a>';
html += '</td>';
html += '</tr>';
html += '</table>';

JSON is stored in a variable, I had to decrease to not get too big here in the example, but in the production model there are other elements. I'm having difficulty making a function that takes this JSON, an HTML, and an array with properties as the parameter, and make the changes.

ex. function call

montaPagina(html,dadosJSON,{"{TITLE_MATERIAL}":"posts.post_title","{SIZE_MATERIAL}":"postmeta.anexo.size.value","{SLUG_TAXONOMY}":"taxonomies.$1.slug","{TAXONOMY}":"taxonomies.$1.name"});

I started creating the function as follows:

montaPagina = function(html,dados,argumentos)
{
    var htmlReturn = '';
    var er = new RegExp(/\[/);
    $.each(dados,function(keyDados,valueDados)
    {
        var htmlTmp = html;

        $.each(argumentos,function(keyArgs,valueArgs)
        {
            var filho = valueArgs.split('.');
            var tmp = valueDados;

            $(filho).each(function(k,v)
            {
                if(er.test(v))
                {
                    vTmp = v.replace(']','');
                    vTmp = vTmp.split('[');
                    tmp = tmp[vTmp[0]];
                    console.log(tmp);
                    /*$(vTmp).each(function(k2,v2)
                    {
                        tmp = tmp[vTmp[0]][v2];
                    });*/

                    //console.log('É array: '+vTmp[0]+"\n\n");
                }else
                {
                    tmp = tmp[v];
                }

                //console.log(tmp);
            });

            console.table(tmp);

            if((typeof tmp !== 'undefined'))
            {
                var regex = new RegExp(keyArgs,"g");
                htmlTmp = htmlTmp.replace(regex,tmp);
            }



        });







       htmlReturn += htmlTmp+"\n\n\n";
    });
    return htmlReturn;
};

But it does not work. If I go straight into the code and type:

var tmp = valueDados.posts.post_title;

I can access the data correctly, however if you try:

var tmp = valueDados.valueArgs;

or

var tmp = valueDados[valueArgs];

or

var tmp = valueDados+"."+valueArgs;

Nothing works!

Could someone help me with this?

    
asked by anonymous 20.07.2016 / 16:24

2 answers

0

So you did not. I was able to resolve by breaking the request and passing as a key, as per the code below.

paginacaoMontaHTML = function(html,dadosJSON,argumentos)
{
    var htmlReturn = '';
    var er = new RegExp(/\[/);
    $.each(dados,function(keyDados,valueDados)
    {
        var htmlTmp = html;
        $.each(argumentos,function(keyArgs,valueArgs)
        {
            var filho = valueArgs.split('.');
            var tmp = valueDados;

            $(filho).each(function(k,v)
            {
                if(er.test(v))
                {
                    vTmp = v.replace(']','');
                    vTmp = vTmp.split('[');
                    keyVTmp = vTmp[0];
                    vTmp.shift();

                    $(vTmp).each(function(k2,v2)
                    {
                        if(typeof(tmp[keyVTmp]) == 'object')
                        {
                            if(typeof(tmp[keyVTmp][v2]) != 'undefined')
                            {
                                tmp = tmp[keyVTmp][v2];
                            }
                        }
                    });
                }else
                {
                    tmp = tmp[v];
                }
            });

            if((typeof tmp !== 'undefined'))
            {
                var regex = new RegExp(keyArgs,"g");
                htmlTmp = htmlTmp.replace(regex,tmp);
            }
        });
       htmlReturn += htmlTmp+"\n\n\n";
    });
    return htmlReturn;
};

Thank you very much and I hope the solution will be useful for other people.

    
21.07.2016 / 21:29
-1

Try as follows

var tmp = JSON.parse(valueDados).posts
    
20.07.2016 / 19:20