Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> JSON encode "true" zamiast true
topcio
post
Post #1





Grupa: Zarejestrowani
Postów: 140
Pomógł: 0
Dołączył: 14.01.2017

Ostrzeżenie: (0%)
-----


Hej

Czy ktoś może mi pomóc dlaczego w wyniku json_encode dosateje

  1. "zmienna_1": "true",


zamiast

  1. "zmienna_1": true,


dobra sorry już znalazłem odpowiedź

wystarczyło dodać do zmiennej (bool)

(IMG:style_emoticons/default/smile.gif)

Nie mniej to nie rozwiązuje mojego problemu, ponieważ dla wartości innych niż true/false również konwertuje mi do bool a tak nie ma być (IMG:style_emoticons/default/sad.gif)
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
topcio
post
Post #2





Grupa: Zarejestrowani
Postów: 140
Pomógł: 0
Dołączył: 14.01.2017

Ostrzeżenie: (0%)
-----


Dobra mój błą

Tak miało być
  1. if (is_numeric($input)) {
  2. settype($input, "integer");
  3. }
  4. if ($input === "true" || $input === "false") {
  5. $input = (bool)$input;
  6. }


teraz jest OK (IMG:style_emoticons/default/smile.gif)
Zmęczenie bierze (IMG:style_emoticons/default/tongue.gif)

Trochę pomęczyłem i wyszło coś takiego

  1. $config_file = file_get_contents($file);
  2. $decoded_data = json_decode($config_file, true);
  3. foreach ($_POST as $POST_key => $POST_value) {
  4. if ($POST_key != "submit" && $POST_key != "ID") {
  5. $POST_key_right = substr($POST_key, strpos($POST_key, "=") + 1);
  6. $POST_key_left = strtok($POST_key, '=');
  7. if (!is_array($POST_value)) {
  8. if ($POST_key_left == "Hunt_") {
  9. foreach($decoded_data as $Primary_Key => $Primary_Val) {
  10. foreach($Primary_Val as $Secondary_Key => $Secondary_Val ) {
  11. if ($Secondary_Key === "Hunt_") {
  12. foreach($Secondary_Val as $Third_Key => $Third_Val ) {
  13. if ($Third_Key == $POST_key_right) {
  14. if ($POST_value == "true") {
  15. $decoded_data[$Primary_Key][$Secondary_Key][$Third_Key] = true;
  16. }
  17. else if ($POST_value == "false") {
  18. $decoded_data[$Primary_Key][$Secondary_Key][$Third_Key] = false;
  19. }
  20. }
  21. }
  22. }
  23. }
  24. }
  25. }
  26. else {
  27. foreach($decoded_data as $Primary_Key => $Primary_Val) {
  28. foreach($Primary_Val as $Secondary_Key => $Secondary_Val ) {
  29. if ($Secondary_Key == $POST_key) {
  30. if (is_numeric($POST_value)) {
  31. settype($POST_value, "integer");
  32. $decoded_data[$Primary_Key][$Secondary_Key] = $POST_value;
  33. }
  34. else if ($POST_value == "true") {
  35. $decoded_data[$Primary_Key][$Secondary_Key] = true;
  36. }
  37. else if ($POST_value == "false") {
  38. $decoded_data[$Primary_Key][$Secondary_Key] = false;
  39. }
  40. else {
  41. $decoded_data[$Primary_Key][$Secondary_Key] = $POST_value;
  42. }
  43. }
  44. }
  45. }
  46. }
  47. }
  48. if (is_array($POST_value)) {
  49. echo "TABLICA - ";
  50. echo $POST_key_left, " - ", $POST_value, "<BR>";
  51. }
  52. }
  53. $enc_data = json_encode($decoded_data, JSON_PRETTY_PRINT, JSON_NUMERIC_CHECK);
  54. file_put_contents($file, $enc_data);


Sugestie mile widziane, jeszcze dane w postaci tablic, może dam radę (IMG:style_emoticons/default/smile.gif)

Ten post edytował topcio 9.12.2022, 21:55:29
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 30.12.2025 - 15:02