1

I try to get this json encoded output from php

$response["success"] = $id;
echo json_encode($response);
echo json_encode($eventid);

while $response only contains an integer at ["success] and $eventid contains an array of ids(for example ["1","3"]). So the php file output is for example like this:

{"success":"3"}["1","3","7"]

Now I try to get both the success, and the eventid. I get the success with

int id = json.getInt("success");

and it works, the problem is to get the array of eventids. I found this post: stackoverflow but it wasn't helpful for me, because I don't have tags for each single element in the array. So the question is whether I do have to add tags for every single element, or there is another Syntax which I can use?

Thanks in advance

Community
  • 1
  • 1
Werdli
  • 235
  • 1
  • 4
  • 11

2 Answers2

5

The provided JSON is not valid. You need to modify it to look like this:

{
    "success": "3",
    "elements": [
        "1",
        "3",
        "7"
    ]
}

If you do that, you will be able to do this:

JSONArray array = json.getJSONArray ("elements");
for (int counter = 0; counter < array.length (); counter ++)
   Log.d ("element", array.getInt (counter));
Shade
  • 9,936
  • 5
  • 60
  • 85
  • Glad to help out. You could mark it as the selected answer if it solved your problem completely. – Shade Mar 12 '13 at 08:37
2

I would encode $eventid within $response just like you did with $success

$response["success"] = $id;
$response["eventid"] = $eventid;
echo json_encode($response);

yields:

{"success":3,"eventid":[ 1, 3, 7]}
jnovack
  • 7,629
  • 2
  • 26
  • 40
  • Well damnit, can't give two people solved can you? How about an upvote if mine wasn't the best answer. – jnovack Mar 12 '13 at 08:54
  • yeah it was definetely a possible answer to my specific problem, but shade's is more universal, for sure i upvoted your's (: – Werdli Mar 12 '13 at 09:13