1

I have developed an api which will post some data in json format to be used in an android app. However I am getting json parsing error. I am new to this whole json thing so unable to understand what the error means.

This is my json encoded output that the php backend generates

   {
    "data": [
        {
            "id": "2",
            "name": "Rice",
            "price": "120",
            "description": "Plain Rice",
            "image": "6990_abstract-photo-2.jpg",
            "time": "12 mins",
            "catagory": "Lunch",
            "subcat": ""
        }
    ]
}{
    "data": [
        {
            "id": "4",
            "name": "Dal",
            "price": "5",
            "description": "dadadad",
            "image": "",
            "time": "20 mins",
            "catagory": "Dinner",
            "subcat": ""
        }
    ]
}{
    "data": [
        "catagory": "Soup"
    ]
}

This is the error the online json parser gives

SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 2 column 1 of the JSON data

What is actually wrong here? Could you please provide me with the correct json output for the following data?

5
  • 2
    There are 3 json objects in the examnple above, if you want to parse all of this you will need to wrap them in [] Commented Oct 20, 2014 at 6:53
  • 4
    Your json code is invalid, because those are 3 different json objects. Can you show us your php code to generate this json result? Commented Oct 20, 2014 at 6:53
  • I have added the php code please help me do the needed modification Commented Oct 20, 2014 at 6:54
  • Your json code is invalid Commented Oct 20, 2014 at 6:55
  • use jsonlint (jsonlint.com} to validate your json Commented Oct 20, 2014 at 6:57

2 Answers 2

2

This should clear it up

    $main = array();
    while($row = $result->fetch(PDO::FETCH_ASSOC)){
        $cat    = $row['category'];
        $query1 = "SELECT * FROM item WHERE catagory='$cat'";       //Prepare login query
        $value  = $DBH->query($query1);
        if($row1 = $value->fetch(PDO::FETCH_OBJ))
        {
            $main[] = array('data'=>array($row1));
        }
        else
        {
            $main[] = array('data'=>array('catagory'=>$row['category']));
        }
    }
    echo json_encode($main);
Sign up to request clarification or add additional context in comments.

Comments

1

You shouldn't create your json string by hand. Create your array structure, then finally invoke json_encode() at the end.

$data = array();

try
{
    $query = "SELECT category FROM category"; // select category FROM category? what?
    $result= $DBH->query($query);
    while($row = $result->fetch(PDO::FETCH_ASSOC)){
        $cat    = $row['category'];
        $query1 = "SELECT * FROM item WHERE catagory='$cat'";
        $value  = $DBH->query($query1);
        if($value->rowCount() > 0) {
            $data[] = array('data' => $value->fetch(PDO::FETCH_ASSOC));
        }
        else {
            $sub = array('category' => $row['category']);
            $data[] = array('data' => $sub);
        }

    }

    $result->closeCursor();
    $DBH = null;

    echo json_encode($data); // encode at the end
}
catch(PDOException $e)
{
    print $e->getMessage ();
    die();
}

2 Comments

Both the table name and field name is category :D I was working late at night my sleep deprived brain just couldn't come up with something not silly :D
@RickRoy well you better get some rest then, code another time, a well rested brain is good

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.