Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

Transformirati jedan json u drugi

[es] :: Ostali programski jezici :: Transformirati jedan json u drugi

[ Pregleda: 2544 | Odgovora: 9 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

nemchus
Niš

Član broj: 325635
Poruke: 19
5.57.75.*



+2 Profil

icon Transformirati jedan json u drugi17.12.2015. u 20:40 - pre 100 meseci
Ćao svima! :D
Pokušavam da napravim web shop. Podatke koje preko php-a primam i koji su u json formatu želim da transformišem tako da mogu lakše da ih prikažem na sajtu.

Ono što dobijam od php-a je:
{name: "product 1", price: "150", picture_path: "product-img1.png", color: "white",…}
{name: "product 1", price: "150", picture_path: "product-img1.png", color: "beige",…}
{name: "product 1", price: "150", picture_path: "product-img1.png", color: "orange",…}
{name: "product 2", price: "55", picture_path: "product-img2.png", color: "white", …}
{name: "product 3", price: "155", picture_path: "product-img3.png", color: "beige",...}

a želim da izgleda ovako:

{name: "product 1", price: "150", picture_path: "product-img1.png", color1: "white", color2:"beige", color3:"orange", colorX:....…}
{name: "product 2", price: "55", picture_path: "product-img2.png", color1: "white", …}
{name: "product 3", price: "155", picture_path: "product-img3.png", color1: "beige",...}

Ili


{name: "product 1", price: "150", picture_path: "product-img1.png", colors: { color1:"white", color2:"beige", color3:"orange"},..…}

Kako bi to moglo da se odradi u jQuery?
"Stop crying Eddie, cuz you can get the fuck out."

"You're gettin' the fuck...
I know you're seven !"

"But you'll be a seven year old
walkin' the dog no house motherfucker !"
 
Odgovor na temu

jablan

Član broj: 8286
Poruke: 4541



+710 Profil

icon Re: Transformirati jedan json u drugi17.12.2015. u 21:11 - pre 100 meseci
Šta radiš kad nije ista cena za plavi i narandžasti product1? Zašto hoćeš da radiš u JS a ne u PHP?
 
Odgovor na temu

nemchus
Niš

Član broj: 325635
Poruke: 19
5.57.75.*



+2 Profil

icon Re: Transformirati jedan json u drugi17.12.2015. u 21:25 - pre 100 meseci
U ovom konkretnom slučaju, jedan produkt može da ima samo različite boje. Zamisao je da se po prelasku mišem preko izlistane boje, promeni slika artikla. Nešto na ovu foru: http://prntscr.com/9ehh3a. Mislio sam da će lakše da mi ide ako u javascriptu na osnovu podataka iz json napravim novi objekat sa podacima. Ono što meni pada na pamet je otprilike ovako:

Code:

function formData(data) {
    var id = {};
    for (var i = 0; i < data.length - 1; i++) {
        for (var j = 1; j < data.length - 1; j++) {
            if (data[i].naziv == data[j].naziv) {

            } else {
                id.naziv = data[i].naziv;
                id.cena = data[i].cena;
                id.slika = data[i].slika;
                id.boje = data[i].boja_naziv;
                id.format_naziv = data[i].format_naziv;
                id.brand_naziv = data[i].brand_naziv;
            }

        }
    
}


"Stop crying Eddie, cuz you can get the fuck out."

"You're gettin' the fuck...
I know you're seven !"

"But you'll be a seven year old
walkin' the dog no house motherfucker !"
 
Odgovor na temu

jablan

Član broj: 8286
Poruke: 4541



+710 Profil

icon Re: Transformirati jedan json u drugi17.12.2015. u 21:36 - pre 100 meseci
Probaj nešto ovako https://jsfiddle.net/zuwqa6qy/
 
Odgovor na temu

nemchus
Niš

Član broj: 325635
Poruke: 19
5.57.75.*



+2 Profil

icon Re: Transformirati jedan json u drugi17.12.2015. u 21:46 - pre 100 meseci
To je to, sad sam razgledao. Hvala ti puno! Par sati se već mačujem sa tim, šta da radim kada sam početnik...Sad ću da razložim sebi i da pokušam da skapiram kako funkcioniše.
"Stop crying Eddie, cuz you can get the fuck out."

"You're gettin' the fuck...
I know you're seven !"

"But you'll be a seven year old
walkin' the dog no house motherfucker !"
 
Odgovor na temu

nemchus
Niš

Član broj: 325635
Poruke: 19
5.57.75.*



+2 Profil

icon Re: Transformirati jedan json u drugi08.01.2016. u 20:54 - pre 100 meseci
Imam još jednu početničku nedoumicu. Ajaxom dovlačim sve podatke i imam funkciju koja mi formatira te podatke onako kako ja želim. Zanima me kako te formatirane podatke mogu da izvučem i iskoristim u drugoj funkciji koja će služiti za filtriranje u odnosu na boju, brand, format itd. U ovom slučaju, Ajax bi samo jedno opalio. Funkcijom globalContent sam hteo da izvučem podatke za dalje filtriranje, a funkcija makeContent pravi html template.

Code:


  function fetchData (){
        $.ajax({
            type: 'post',
            url: 'submit.php',
            dataType : 'json',
            cache: false,
            success: function(records){
                globalContent(formatData(records));
            }
        });
    }

function formatData(data) {
    var newdata = {};
    data.forEach(function(e){
        newdata[e.naziv] = newdata[e.naziv] || {name: e.naziv, price: e.cena, colors: [],
            photos: [], brand: e.brand_naziv, format: e.format_naziv};

        newdata[e.naziv].colors.push(e.boja_naziv);
        newdata[e.naziv].photos.push(e.slika);

    });


    return(
        JSON.stringify(
            Object.keys(newdata).map(
            function(key){return newdata[key]}
        )));
}


  function globalContent (result) {


        

    }


$('.products-section').html(makeContent(jQuery.parseJSON(FILTRIRANO)));

"Stop crying Eddie, cuz you can get the fuck out."

"You're gettin' the fuck...
I know you're seven !"

"But you'll be a seven year old
walkin' the dog no house motherfucker !"
 
Odgovor na temu

nemchus
Niš

Član broj: 325635
Poruke: 19
5.57.75.*



+2 Profil

icon Re: Transformirati jedan json u drugi10.01.2016. u 18:43 - pre 100 meseci
Ovo je ono do čega sam došao i što koliko toliko funkcioniše. Pitanje je kako mogu da ga prepravim tako da se ova funkcija uključuje svaki put kada ubacim novi element u niz filterOpts. On se puni sa id čekiranih polja na UI...

Code:

function fetchData (){
        $.ajax({
            type: 'post',
            url: 'submit.php',
            dataType : 'json',
            cache: false,
            success: function(records){
                globalContent(jQuery.parseJSON(formatData(records)));
            }
//            success: callback
        });
    }


Array.prototype.contains = function(obj) {
        var i = this.length;
        while (i--) {
            if (this[i] == obj) {
                return true;
            }
        }
        return false;
    }

    function globalContent (result) {

            var results = [];
            var filterOpt = [];
        filterOpt.push("beige");
            for (var x = 0; x < result.length; x++) {
                for (var i = 0; i < filterOpt.length; i++) {

                        if (result[x].colors[i] !== null) {

                            if (result[x].colors.contains(filterOpt[i]) || filterOpt[i] == result[x].brand || filterOpt[i] == result[x].format) {
                                if (results.contains(result[x]) === false) {
                                    results.push(result[x]);
                                    }
                                }
                        }
                }
            }
        $('.products-section').html(makeContent(results));

    }


"Stop crying Eddie, cuz you can get the fuck out."

"You're gettin' the fuck...
I know you're seven !"

"But you'll be a seven year old
walkin' the dog no house motherfucker !"
 
Odgovor na temu

jablan

Član broj: 8286
Poruke: 4541



+710 Profil

icon Re: Transformirati jedan json u drugi10.01.2016. u 20:33 - pre 100 meseci
Ja ti ne mogu pomoći, imam mentalnu blokadu čim naiđem na C-ovsku for petlju. :) Mogu samo da ti preporučim da vidiš sa moderatorom da prebaci poruku u adekvatni podforum (za JS), kao i da postaviš primer koji funkcioniše na jsfiddle.
 
Odgovor na temu

nemchus
Niš

Član broj: 325635
Poruke: 19
5.57.75.*



+2 Profil

icon Re: Transformirati jedan json u drugi10.01.2016. u 21:07 - pre 100 meseci
Ma opušteno, ja sam u međuvremenu sam došao do nekog rešenja ;) Hvala ti u svakom slučaju. Trebalo bi da prebacim ovu diskusiju u JS podforum, definitivno.
"Stop crying Eddie, cuz you can get the fuck out."

"You're gettin' the fuck...
I know you're seven !"

"But you'll be a seven year old
walkin' the dog no house motherfucker !"
 
Odgovor na temu

luke6

Član broj: 337493
Poruke: 2
123.24.56.*



Profil

icon Re: Transformirati jedan json u drugi06.11.2017. u 07:59 - pre 77 meseci
Kipróbálhatja ezt az eszközt: json formatter
 
Odgovor na temu

[es] :: Ostali programski jezici :: Transformirati jedan json u drugi

[ Pregleda: 2544 | Odgovora: 9 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.