Ngorld
Ngorld

Reputation: 856

How to get a json group

I have json like this :

jsons={"books":[
    {"book_1":[ 
        {"title":"DATA 1"},
        {"author":"DATA 2"},
        {"category":"DATA 3"} 
    ]}, 
    {"book_2":[ 
        {"title":"DATA 4"},
        {"author":"DATA 5"},
        {"category":"DATA 6"}
    ]}, 
]};

I want to get only book_1 group with jQuery $.each. How to do it ?

And I have 2 array in PHP

$array_1 = ("A","B","C");
$array_2 = ("D","E","F");

I want json_encode like group :

jsons={"array_0":[
    {"array_1":[ 
        {"1":"A"},
        {"2":"B"},
        {"3":"c"} 
    ]}, 
    {"array_2":[ 
        {"1":"D"},
        {"2":"E"},
        {"2":"F"}
    ]}, 
]};

How to do it.

Thanks you.

Upvotes: 0

Views: 38

Answers (1)

Rohan Kumar
Rohan Kumar

Reputation: 40639

Try this,

In jQuery

console.log(jsons.books[0].book_1);

Using $.each()

$(jsons.books).each(function (i, v) {
    console.log(v);
});

In PHP

echo json_encode(array('array_0'=>
        array('array_1'=>$array_1,'array_2'=>$array_2)
     ));

Upvotes: 2

Related Questions