0

I have some issue with a decoded Json object sended to a php file. I have tried some different format like this

{"2":"{Costo=13, ID=9, Durata_m=25, Descrizione=Servizio 9}","1":"{Costo=7, ID=8, Durata_m=20, Descrizione=Servizio 8}"}

or this.

[{"Costo":"7.5","ID":"3","Durata_m":"30","Descrizione":"Barba Rasoio"},{"Costo":"4.5","ID":"4","Durata_m":"20","Descrizione":"Barba Macchinetta"}]

In order the first, any suggestions helps me, then i have converted previous string using GSON, however php doesn't decode.

This is my php:

//Receive JSON
 $JSON_Android = $_POST["JSON"];
//Decode Json
$data = json_decode($JSON_Android, true);
foreach ($data['servizi'] as $key => $value) 
    { echo "Key: $key; Value: $value<br />\n";
 }

How can I access single elements of array? What I'm doing wrong? Thanks in advance

6
  • What is show in echo "FROM PHP: " . $value; ? Commented Oct 7, 2015 at 18:18
  • I haven't code here, but if I remember correctly, it's the string above without initial number {"1": Commented Oct 7, 2015 at 19:03
  • Well then check the answer.. Commented Oct 7, 2015 at 19:04
  • For sure! I will test it tomorrow. Commented Oct 7, 2015 at 19:17
  • comment for result please.. Commented Oct 7, 2015 at 19:35

2 Answers 2

1

I think you should check the content in this way

//Receive JSON
$JSON_Android = $_POST["JSON"];
//Decode Json
$data = json_decode($JSON_Android, true);
foreach ($data as $key => $value) { 
    echo "FROM PHP:  " . $value;

    echo "Test : " .$value['ID'];

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

2 Comments

Here I am. This is the output of echo test: <b>Warning</b>: Illegal string offset 'ID' in <b>D:\...etc
bold 'It Works!' Using string converted by GSON in this format [{"Costo":"7.5","ID":"3","Durata_m":"30","Descrizione":"Barba Rasoio"}] I'm able to access to elements of array. Thank you
0

your elements of {Costo=7, ID=8, Durata_m=20, Descrizione=Servizio 8}

are not properly formated as an array element. That is a pure string and not an array value.

This is a json object with 1 element of array:

{
  "1": {
    "Costo": 7,
    "ID": 8,
    "Durata_m": 20
  }
}

The Inside are json objects. Therefore your json string was not properly formated to operate with that logic. What you had was an element of a string. That is the reason why it was a valid json (passing jsonlint) but was not the correct one that you wanted to use.

UPDATE Because this format is fix, I have a non-elegant way:

//Receive JSON
$JSON_Android = $_POST["JSON"];
//Decode Json
$data = json_decode($JSON_Android, true);
foreach ($data as $key => $value) { 
    //to separate each element
    $newArray = explode(',',$value);
    $newItem = explode('=', $newArray[1]);
    echo "ID". $newItem[1];
}

That would be the dirty way to do it ONLY IF THE PLACEMENT OF DATA IS FIX. (ie the second element of the first explode is always ID.

I will leave it to someone else to make the suggested code better. I would recommend more to ensure that the json you are receive is proper because as I explained, it is incorrectly formated and as an api developer, you want an adaptive way for any given client to use the data efficiently.

4 Comments

Thank you for response. I had imagined that problem was just this string, but i don't know how to change or reformat it. It cames from a listview selection on android listview, and i have followed some tutorial to convert to json object. Have you any suggest or solution?
@Jayelef I added more detail and suggested code with explanation, but it is not the 100% solution, you will need to take it and work with it.
Well, while you had suggested your code, i'm trying to change method to create the array. I'm just working yet, however i have copied your snippet and i have tested it. This is the output: <b>Warning</b>: explode() expects parameter 2 to be string, array given in <b>D:\inetpub...etc
Using GSON library, i have converted the string to send in this format [{"Costo":"4.5","ID":"4","Durata_m":"20","Descrizione":"Barba Macchinetta"},{"Costo":"7.5","ID":"3","Durata_m":"30","Descrizione":"Barba Rasoio"}], but now i'm not able to decode it in php file. Always error. Blob...(

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.