javascript - Valid JSON to display data in angularJs


I want to display data(json) in my site using AngularJs . here's what i did :
Create a database in phpmyAdmin .
Create a table with 2 row , subject and body . Should i create an id ?
After doing with PHP and angular , I got JSON like this :

    ,{"0":"Hello","subject":"Hello","1":"This is chandler !","body":"This is chandler !"}
    {"0":"Something new in website","subject":"Something new in website","1":"oh Awsome !","body":"oh Awsome !"

I think this is invalid JSON because when I replace it with custom JSON that I wrote it work .

Json valid

    "fruits": [
            "id": "1",
            "name": "Apple"
            "id": "2",
            "name": "Orange"


var fruitsApp = angular.module('fruitsApp', []);

fruitsApp.factory('fruitsFactory', function($http) {
    return {
        getFruitsAsync: function(callback) {

fruitsApp.controller('fruitsController', function($scope, fruitsFactory) {
    fruitsFactory.getFruitsAsync(function(results) {
        console.log('fruitsController async returned value');
        $scope.fruits = results.fruits;


            <li ng-repeat="fruit in fruits">
                {{fruit.subject}} is {{fruit.body}}



$data = json_decode(file_get_contents("php://input"));
$subject = mysql_real_escape_string($data->subject);
$body = mysql_real_escape_string($data->body);
mysql_select_db("angular") or die(mysql_error());
mysql_query("INSERT INTO newstory (subject,body) VALUES ('$subject', '$body')");
Print "Your information has been successfully added to the database.";

$query = "SELECT * FROM newstory";
$result = mysql_query($query);

$arr = array();
while ($row = mysql_fetch_array($result)) {
    $subject = $row['subject'];
    $body = $row['body'];
    $arr[] = $row;
echo json_encode($arr);

Any idea ? Thx in advance




Your JSON is a valid. Refer to this for information on JSON and this to check/validate a JSON object.

The data coming back from your $http.get / database data does not have a{-code-1} attribute and you expect that when you set your$scope.{-code-1} (the below snippet is taken from your code):

 $scope.{-code-1} = results.{-code-1};

The structure of the data that is being returned by the $http.get call is different than the format of your sample data.

Here's your $http.get / database data (I shortened it for brevity):

    "0": "Soheil",
    "1": "Sadeghbayan",
    "subject": "Soheil",
    "body": "Sadeghbayan"
    "0": "Hello",
    "1": "This is chandler !",
    "subject": "Hello",
    "body": "This is chandler !"
    "0": "",
    "1": "",
    "subject": "",
    "body": ""

And here's your sample / mock data:

"{-code-1}": [
        "id": "1",
        "name": "Apple"
        "id": "2",
        "name": "Orange"

The former is an array of objects with keys:0,1,subject andbody. The latter is an object with keys:{-code-1}.

They are both valid JSON objects with different object structures. But, you expect a{-code-1} attribute where there isn't one. Also, your HTML/UI might be expecting the data format to look like what is in your mock data. So check that too.

People are also looking for solutions to the problem: php - Send files with phpmailer before uploading them?


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.