0

I need to communicate with my parser json in my java class to populate a gridview with images. The php code bellow is outputing:

{"posts":["http://fisioterapeutacamila.com/iMoveis/img/apto01.jpg",
"http://fisioterapeutacamila.com/iMoveis/img/apto01.jpg",
"http://fisioterapeutacamila.com/iMoveis/img/apto01.jpg",
"http://fisioterapeutacamila.com/iMoveis/img/apto01.jpg",    
"http://fisioterapeutacamila.com/iMoveis/img/apto01.jpg"]}

How should write it to set the same key value "images" to output?:

{"posts":[{"images":"http://fisioterapeutacamila.com/iMoveis/img/apto01.jpg", 
"images":"http://fisioterapeutacamila.com/iMoveis/img/apto01.jpg",
"images":"http://fisioterapeutacamila.com/iMoveis/img/apto01.jpg",
"images":"http://fisioterapeutacamila.com/iMoveis/img/apto01.jpg",
"images":"http://fisioterapeutacamila.com/iMoveis/img/apto01.jpg"}]}

PHP file:

$sql = "SELECT * FROM iMoveis";
$result = mysqli_query($mysqli, $sql);

$response = array();
$images = array();

while($row = mysqli_fetch_assoc($result)){

  $images[] = $row['img1'];
  $images[] = $row['img2'];
  $images[] = $row['img3'];
  $images[] = $row['img4'];
  $images[] = $row['img5'];

}

$response['posts'] = $images;

echo json_encode($response, JSON_UNESCAPED_SLASHES);
Pang
  • 9,564
  • 146
  • 81
  • 122
madsongr
  • 653
  • 1
  • 11
  • 29

1 Answers1

0

It's not valid to have the same key twice in an object. They will just overwrite eachother. Best you can do is make it a key in your array:

$images = array('images' => array());

while($row = mysqli_fetch_assoc($result)){

  $images['images'][] = $row['img1'];
  $images['images'][] = $row['img2'];
  $images['images'][] = $row['img3'];
  $images['images'][] = $row['img4'];
  $images['images'][] = $row['img5'];
}

which will output something like this (note the escaping too):

{
    "posts":{
        "images":[
            "http:\/\/fisioterapeutacamila.com\/iMoveis\/img\/apto01.jpg",
            "http:\/\/fisioterapeutacamila.com\/iMoveis\/img\/apto01.jpg",
            "http:\/\/fisioterapeutacamila.com\/iMoveis\/img\/apto01.jpg",
            "http:\/\/fisioterapeutacamila.com\/iMoveis\/img\/apto01.jpg"
        ]
    }
}

As per comments, an array of objects:

$images = array();

while ($row = mysqli_fetch_assoc($result)) {
    $images[] = array('image' => $row['img1']);
    $images[] = array('image' => $row['img2']);
    $images[] = array('image' => $row['img3']);
    $images[] = array('image' => $row['img4']);
    $images[] = array('image' => $row['img5']);
}

producing the following output:

{

    "posts":[
        {
            "image":"http:\/\/fisioterapeutacamila.com\/iMoveis\/img\/apto01.jpg"
        },
        {
            "image":"http:\/\/fisioterapeutacamila.com\/iMoveis\/img\/apto01.jpg"
        },
        {
            "image":"http:\/\/fisioterapeutacamila.com\/iMoveis\/img\/apto01.jpg"
        },
        {
            "image":"http:\/\/fisioterapeutacamila.com\/iMoveis\/img\/apto01.jpg"
        },
        {
            "image":"http:\/\/fisioterapeutacamila.com\/iMoveis\/img\/apto01.jpg"
        }
    ]

}
rjdown
  • 9,162
  • 3
  • 32
  • 45
  • 1
    ok, but this is not the output the I need. I need to find de string "images" in my java class. Not the array "images" – madsongr Oct 29 '15 at 19:41
  • As I said, the output you "need" is invalid. It is impossible for json_encode to output this. – rjdown Oct 29 '15 at 19:46
  • ok, please take a look here http://javatechig.com/?json=get_recent_posts&count=45. Inside array "posts" there is the array "attachments" with strings "url". I made it more simple removing "attachments" and changing "url" to "images". On each "url" he gets the value. – madsongr Oct 29 '15 at 19:54
  • 1
    That is not the same format at all. There, you have an array of objects. In your example, you have an array containing a single object. I will update my answer to show a solution in the format of the link – rjdown Oct 29 '15 at 19:58
  • 1
    I recommend using a site like this to see the different structures more clearly: http://json.parser.online.fr/ – rjdown Oct 29 '15 at 20:09