JSON from php / MySQL

GeX

GeX

Soldato
Joined
17 Dec 2002
Posts
6,895
Location
Manchester
Hi all. JSON isn't something that I've used much before - but it seems to have advantages over XML when parsing on an Android device.

I need a script to return a list of ID numbers. I've done this, but I'm not sure if the output I'm generating is 'correct'. I want it to be slightly different.. but I'm not sure if what I think it should look like is correct either.

Basically, I don't want to spend any time trying to fudge it into working one way and then find out it was fine before.

Code:
	$messageids = array();
	$result = @mysql_query($findsql);
	$rowCount = mysql_num_rows($result);
	//add the results into an array - but don't go beyond the limit
	if ($rowCount) {
		while (($messageid = mysql_fetch_assoc($result)) && count($messageids) < $limit){
		$messageids[] = $messageid['id'];
	    }
	    //if there are more rows than we're returning then set a flag to tell the device it needs to check again once it has finished
	    if ($rowCount > $limit){
		$messageids['list'] = "Incomplete";
		} else {
		$messageids['list'] = "Complete";
		}
	}
	header('Content-Type: application/json');
	echo json_encode(array("MessageIDs" => $messageids));

That results in a return like this;

Code:
{
MessageIDs: {
0: "102739",
1: "102740",
2: "102741",
3: "102742",
4: "24112476",
5: "24112478",
6: "24112480",
7: "24112482",
8: "24112483",
9: "24112484",
list: "Incomplete"
}
}

If I change this line
Code:
$messageids[] = $messageid['id'];
to
Code:
$messageids[] = $messageid;

then I get
Code:
{
MessageIDs: {
0: {
id: "102739"
},
1: {
id: "102740"
},
2: {
id: "102741"
},
3: {
id: "102742"
},
4: {
id: "24112476"
},
5: {
id: "24112478"
},
6: {
id: "24112480"
},
7: {
id: "24112482"
},
8: {
id: "24112483"
},
9: {
id: "24112484"
},
list: "Incomplete"
}
}

What I think I want is similar to the second, but without the numerical index. On the client device I need to loop through and do something with each ID number.

As I say though.. I'm not too sure how this should look.

http://stackoverflow.com/questions/9605913/how-to-parse-json-in-android
 
Soldato
Joined
12 May 2007
Posts
3,896
Location
Bristol
You're quite close. Either of the following two structures would work for you.

Code:
{
    "MessageID": [
        {
            "id": "102739"
        },
        {
            "id": "102740"
        },
        {
            "id": "102741"
        },
        {
            "id": "102742"
        }
    ]
}

You'll return the json, grab the messageID array and loop through all the objects inside of it.

Code:
[
    {
        "id": "102739"
    },
    {
        "id": "102740"
    },
    {
        "id": "102741"
    },
    {
        "id": "102742"
    }
]
Here, the returned json is an array, so you can just loop through all the objects inside of it.

I'd recommend the first format, as it's easier to add to it if you want.
 
Associate
Joined
20 Jul 2011
Posts
128
Location
London, UK
I think the reason you are getting numeric indices is due to the fact you are mixing automatically generated numeric indices and an associative index in the same array

PHP:
	$messageids = array();
	$result = @mysql_query($findsql);
	$rowCount = mysql_num_rows($result);
	//add the results into an array - but don't go beyond the limit
	if ($rowCount) {
		while (($messageid = mysql_fetch_assoc($result)) && count($messageids) < $limit){
                // automatic numeric index
		$messageids[] = $messageid['id'];
	    }
	    //if there are more rows than we're returning then set a flag to tell the device it needs to check again once it has finished
	    if ($rowCount > $limit){
                // associative index
		$messageids['list'] = "Incomplete";
		} else {
		$messageids['list'] = "Complete";
		}
	}
	header('Content-Type: application/json');
	echo json_encode(array("MessageIDs" => $messageids));

If you separate the message id list and the 'list' message like so:

PHP:
	if ($rowCount) {
		while (($messageid = mysql_fetch_assoc($result)) && count($messageids) < $limit){
			$messageids[] = $messageid['id'];
	    }
	    //if there are more rows than we're returning then set a flag to tell the device it needs to check again once it has finished
	    if ($rowCount > $limit){
			$list = "Incomplete";
		} else {
			$list = "Complete";
		}
	}
	header('Content-Type: application/json');
	echo json_encode(array("MessageIDs" => $messageids,"list" => $list));

you should get JSON output that looks like

Code:
{
	"MessageIDs" : [
		"24234242",
		"24242121",
		"34905935",
		"69420942",
		"82347284",
		"11231231",
		"93459394",
		"35838277",
		"90242042"
	],
	"list" : "complete"
}
 
Last edited:

GeX

GeX

Soldato
OP
Joined
17 Dec 2002
Posts
6,895
Location
Manchester
Thanks for the clarification guys. I've split out the arrays as Oz!asM!dwinter suggested and have the device parsing the ID list as I wanted.
 
Back
Top Bottom