简体   繁体   中英

json_decode not working

I have the following JSON:

{"easyStoreRelayList":[],"DecathlonStoreAreaNoStoreInGroup":"Nella zona che hai selezionato nessun negozio Decathlon al momento effettua il servizio \"ACQUISTA ON LINE RITIRA IN NEGOZIO\" oppure non sono presenti negozi Decathlon.","warehouseRelayList":[],"DecathlonStoreAreaRememberMe":"Riconoscimi alla prossima visita","DecathlonWarehouseParcelSection":"Magazzini","DecathlonStoreParcelSection":"Negozio DECATHLON","DecathlonEasyStoreParcelSection":"Negozio DECATHLON EASY","geoName":"Alessandria","physicalStoreList":[["PS_IT_45","Serravalle Scrivia (Alessandria)","471",null,"7",null]]}

This is my code to try and have it in json format so that it's easier to me to work whit the info.

//$html is the var containing the original string befora trying to parse it to JSON

    $html = stripslashes(str_replace('\"', '"', $html));
    $obj = json_decode($html,true);
    $obj = rtrim($obj, "\0");


    echo $obj["DecathlonStoreAreaNoStoreInGroup"];
    if(json_last_error() == 4){
        echo "ERROR";
    }

I keep getting the error of: Illegal string offset 'DecathlonStoreAreaNoStoreInGroup', and the error 4 as "json_last_error".

Anyone kwnos what it could be, that is not parsing corretly the json??

Your first problem is this line:

$html = stripslashes(str_replace('\"', '"', $html));

You do not need to do any of this stuff when parsing JSON. If the JSON is valid, then the above line will make it invalid. If it is invalid when you get it, then nothing in that line will be enough to fix it.

So the first thing to do is remove that line.

As it turns out, your JSON is also invalid. It contains double-quote characters in a string that is bounded by double-quotes.

"Nella zona che hai selezionato nessun negozio Decathlon al momento effettua il servizio "ACQUISTA ON LINE RITIRA IN NEGOZIO" oppure non sono presenti negozi Decathlon."

This part of your JSON is not valid, and cannot be parsed by json_encode() .

However, you can't easily fix this in your PHP program. You need to fix the system that is generating this invalid JSON. Once it arrives in your PHP code as an invalid string, there's not much you can do about it.

"Nella zona che hai selezionato nessun negozio Decathlon al momento effettua il servizio "ACQUISTA ON LINE RITIRA IN NEGOZIO" oppure non sono presenti negozi Decathlon."

Here. Look you have used the same quotes inside of your main quotes so AQUISTA ON LINE... is considered to be out of the string. Transform it to :

"Nella zona che hai selezionato nessun negozio Decathlon al momento effettua il servizio 'ACQUISTA ON LINE RITIRA IN NEGOZIO' oppure non sono presenti negozi Decathlon."

Edit : $html = stripslashes(str_replace('\\"', '"', $html)); Didn't you mean something like $html = stripslashes(str_replace('"', '\\"', $html));

"DecathlonStoreAreaNoStoreInGroup":"Nella zona che hai selezionato nessun negozio Decathlon al momento effettua il servizio "ACQUISTA ON LINE RITIRA IN NEGOZIO" oppure non sono presenti negozi Decathlon."

Above script has " within string which is invalid JSON format. To make it correct put a slash before " within string like this.

"DecathlonStoreAreaNoStoreInGroup":"Nella zona che hai selezionato nessun negozio Decathlon al momento effettua il servizio \"ACQUISTA ON LINE RITIRA IN NEGOZIO\" oppure non sono presenti negozi Decathlon."

Valid JSON would be:

{  
   "easyStoreRelayList":[  

   ],
   "DecathlonStoreAreaNoStoreInGroup":"Nella zona che hai selezionato nessun negozio Decathlon al momento effettua il servizio \"ACQUISTA ON LINE RITIRA IN NEGOZIO\" oppure non sono presenti negozi Decathlon.",
   "warehouseRelayList":[  

   ],
   "DecathlonStoreAreaRememberMe":"Riconoscimi alla prossima visita",
   "DecathlonWarehouseParcelSection":"Magazzini",
   "DecathlonStoreParcelSection":"Negozio DECATHLON",
   "DecathlonEasyStoreParcelSection":"Negozio DECATHLON EASY",
   "geoName":"Alessandria",
   "physicalStoreList":[  
      [  
         "PS_IT_45",
         "Serravalle Scrivia (Alessandria)",
         "471",
         null,
         "7",
         null
      ]
   ]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM