I've to create in android like this,
"draft":
[
{ "firstName": "Glen", "lastName": "McGrath" },
{ "firstName": "Steve", "lastName": "Vaugh" }
]
How can I create this type of JSON? I've been through some links but not getting correct output.
So far, I've tried this code,
JSONObject newObj = null ;
for(int h=0; h<3; h++)
{
newObj = new JSONObject();
newObj.put("firstName", "" + h);
newObj.put("lastName", "" + h);
}
JSONArray postjson = new JSONArray();
postjson.put(newObj);
But, not getting any output while decoding this json in php file.
Below is my .php file to decode json,
$json = $_SERVER['HTTP_JSON'];
$json = json_decode($json, true);
mysql_connect("localhost", "root", "") or die(mysql_error()) ;
mysql_select_db("retail_menu") or die(mysql_error()) ;
foreach($json as $value)
{
foreach($value as $value1)
{
echo $data1 = $value1['firstName'];
echo $data2 = $value1['lastName'];
$query = "INSERT INTO `rr_friendid` (friend_id, friend_name) VALUES ('$data1','$data2')";
var_dump($query);
mysql_query($query);
}
}
But, I can't see any values store in database. I don't know where I'm wrong, in creating json or while decoding it. How can I create json as the above I mentioned?
The following json you require is not valid:
"draft":
[
{ "firstName": "Glen", "lastName": "McGrath" },
{ "firstName": "Steve", "lastName": "Vaugh" }
]
It should have an enclosing braze.
{
"draft": [
{
"firstName": "Glen",
"lastName": "McGrath"
},
{
"firstName": "Steve",
"lastName": "Vaugh"
}
]
}
To check valid json format, see onlineJsonEditor.