1

I have modified my code over a couple of hours and I just cannot seem to parse value from android and use it in php, I alway get JSONarray cannot be converted to JSONobject.

<?php

include 'config.php';

$con=mysql_connect("$servername", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$dbname")or die("cannot select DB");

if( isset($_POST["json"]) ) {

     $value = $_POST["json"]; 
     $decoded_value = json_decode($value);

    $sql = "SELECT * from table WHERE Code = '".$decoded_value->{'msg'}."'";
    $result = mysql_query($sql);
    $json = array();

    if(mysql_num_rows($result)){
        while($row=mysql_fetch_assoc($result)){
        $json['myarray'][]=$row;
        }
    }

mysql_close($con);
echo json_encode($json); 

}
?> 
1
  • Is that a mysql_* function I see.. Commented Nov 27, 2015 at 19:07

1 Answer 1

1
json_encode($json, JSON_FORCE_OBJECT)

The json_encode function has multiple parameters that you can pass in order to modify the encoding method (check it out here). In your instance, there is the option to force the encoding to be in an object form, rather than the default of either array or object.

Sign up to request clarification or add additional context in comments.

1 Comment

While this code may answer the question, it would be better to explain how it solves the problem and why to use it. Code-only answers are not useful in the long run.

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.