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
 

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