2017-01-14 7 views
1

私のAndroid開発者は以下のようなJSONレスポンスが必要です。PHPのJSON API

[ 
{ "StudentId":"1", "StudentName":"Rahul", "StudentMarks":"83" }, { "StudentId":"2", "StudentName":"Rohit", "StudentMarks":"91"} ] 

JSON APIを生成するための私の現在のコードは以下の通りです。

<?php 
 
$response = array(); 
 
require 'db.php'; 
 

 
$query = "SELECT * FROM news order by id desc"; 
 
\t 
 

 
    $result = mysqli_query($conn,$query); 
 
    
 
    if (mysqli_num_rows($result) > 0) { 
 
     
 
    $response["news"] = array(); 
 
    
 
    while ($row = $result->fetch_assoc()) { 
 
       
 
      $news= array(); 
 
      $news["id"] = $row["id"]; 
 
      $news["title"] = $row["title"];  
 
      $news["description"] = $row["description"];     
 
      
 
      array_push($response["news"], $news); 
 
      } 
 
      $response["success"] = 1; 
 

 
    // echoing JSON response 
 
    echo json_encode($response); 
 
} else { 
 

 
    $response["success"] = 0; 
 
    echo json_encode($response); 
 
}

私は応答

{"news":[{"id":"1","title":"My first news title here","description":"My first news description will be here"}],"success":1}

以下のように私は多くの変更を試してみましたが、上記のように、適切な結果を得ていない取得しています。私はそれを必要な応答にするために私の応答から "ニュース"と "成功"を取り除きたい。最初に見つかったコードのような応答を得るためには、どうすればよいですか?

おかげ

+0

このコードを試してみてください? – C2486

+0

@Rishi私は、私が得ている反応を追加しました。 –

+0

なぜこの '[{{'?それは '[{'?私は3つのオープニングと2つの閉じ括弧しか見ることができません – C2486

答えて

1

はsucess`応答 `について何

<?php 
$response = array(); 
require 'db.php'; 

$query = "SELECT * FROM news order by id desc"; 


    $result = mysqli_query($conn,$query); 

    if (mysqli_num_rows($result) > 0) { 

    $response["news"] = array(); 

    while ($row = $result->fetch_assoc()) { 

      $arr[]=array('StudentId'=>$row["id"],'title'=>$row["title"],'description'=>$row["description"]) 
      } 
      $response =$arr ; 

    // echoing JSON response 
    echo json_encode($response); 
} else { 

    $response = array('success'=>0); 
    echo json_encode($response); 
}