Answers for "string to json php"

PHP
3

string json format to object in php

<?php
  // JSON string
  $someJSON = '[{"name":"Jonathan Suh","gender":"male"},{"name":"William Philbin","gender":"male"},{"name":"Allison McKinnery","gender":"female"}]';

  // Convert JSON string to Array
  $someArray = json_decode($someJSON, true);
  print_r($someArray);        // Dump all data of the Array
  echo $someArray[0]["name"]; // Access Array data

  // Convert JSON string to Object
  $someObject = json_decode($someJSON);
  print_r($someObject);      // Dump all data of the Object
  echo $someObject[0]->name; // Access Object data
?>
Posted by: Guest on June-26-2020
15

json php

//Json Encode

$person = array( 
    "name" => "KINGASV", 
    "title" => "CTO"
); 
$personJSON=json_encode($person);//returns JSON string

//Json Decode

$personJSON = '{"name":"KINGASV","title":"CTO"}';

$person = json_decode($personJSON);

echo $person->name; // KINGASV
Posted by: Guest on July-16-2020
19

php json_decode

$personJSON = '{"name":"Johny Carson","title":"CTO"}';

$person = json_decode($personJSON);

echo $person->name; // Johny Carson
Posted by: Guest on July-01-2019
0

json to array php

//2 ways
  //this is for string from $_REQUEST,$_POST to array
$jsonText = $_REQUEST['myJSON'];
$decodedText = html_entity_decode($jsonText);
$myArray = json_decode($decodedText, true);

//this is for json to array
$assosiative_array = json_decode(json_encode($jsonText),true);
Posted by: Guest on July-26-2021
1

json to array php

<?php

$myArr = '{
  "neworder": {
    "-newfolder": "NO",
    "auth": {
      "-extra": "8",
      "-login": "login",
      "-pass": "pass"
    },
    "order": {
      "-orderno": "111111",
      "barcode": "111111",
      "sender": {
        "company": "Ministry of Internal Affairs",
        "person": "I. I. Ivanov",
        "phone": "123-45-67",
        "town": "Saint-Petersburg",
        "address": "Petrovka Str., 38, room 35",
        "date": "2021-03-22",
        "time_min": "09:00",
        "time_max": "14:00"
      },
      "receiver": {
        "company": "Ministry of Internal Affairs",
        "person": "Tom Wale",
        "phone": "123-45-67",
        "zipcode": "125480",
        "town": {
          "-regioncode": "78",
          "-country": "RU",
          "#text": "Saint-Petersburg"
        },
        "address": "Petrovka Str., 38, room 35",
        "pvz": "124",
        "inn": "1112223335",
        "date": "2021-03-22",
        "time_min": "09:00",
        "time_max": "14:00",
        "deliveryPIN": "1234",
        "coords": {
          "-lat": "55.680327",
          "-lon": "37.604456"
        }
      },
      "return": "NO",
      "weight": "5.1",
      "return_weight": "5.1",
      "quantity": "2",
      "paytype": "CASH",
      "service": "2",
      "return_service": "1",
      "type": "3",
      "return_type": "3",
      "courier": "22",
      "price": "387.5",
      "deliveryprice": {
        "-VATrate": "20",
        "#text": "150"
      },
      "inshprice": "387.5",
      "receiverpays": "NO",
      "discount": "120",
      "enclosure": "Kids toys",
      "instruction": "Check in the presence of the buyer, sign acceptance certificate",
      "department": "Accounting",
      "pickup": "NO",
      "acceptpartially": "NO",
      "costcode": "cc12345",
      "items": {
        "item": [
          {
            "-extcode": "abc123",
            "-quantity": "1",
            "-mass": "0.2",
            "-retprice": "37.5",
            "-VATrate": "0",
            "-barcode": "2345625213125",
            "-textArticle": "1",
            "-article": "1",
            "-volume": "3",
            "-origincountry": "AUT",
            "-GTD": "321546654",
            "-excise": "15.20",
            "-suppcompany": "LLC \"Miller and Partners\"",
            "-suppphone": "79161234567",
            "-suppINN": "1112223334",
            "-governmentCode": "11223311",
            "#text": "Race car"
          },
          {
            "-extcode": "abc124",
            "-quantity": "2",
            "-mass": "2",
            "-retprice": "100",
            "-inshprice": "100",
            "-VATrate": "10",
            "-barcode": "4645625213138",
            "-article": "2",
            "-length": "10",
            "-width": "20",
            "-height": "30",
            "-origincountry": "004",
            "#text": "Princess castle"
          },
          {
            "-extcode": "abc125",
            "-quantity": "3",
            "-mass": "0.3",
            "-retprice": "50",
            "-inshprice": "50",
            "-barcode": "2345625213126",
            "-itemcode": "44123",
            "-article": "3",
            "-type": "1",
            "#text": "Clay mass"
          }
        ]
      },
      "packages": {
        "package": [
          {
            "-strbarcode": "ORD0000001",
            "-mass": "1",
            "-message": ""
          },
          {
            "-strbarcode": "ORD0000002",
            "-mass": "2.5",
            "-message": "",
            "-length": "10",
            "-width": "20",
            "-height": "30"
          }
        ]
      },
      "deliveryset": {
        "-above_price": "100",
        "-return_price": "1000",
        "-VATrate": "10",
        "below": [
          {
            "-below_sum": "500",
            "-price": "500",
            "-self-closing": "true"
          },
          {
            "-below_sum": "2000",
            "-price": "300",
            "-self-closing": "true"
          }
        ]
      },
     
      "overall_volume": "81",
      "userid": "123",
      "groupid": "456"
    }
  }
}';
    print_r(json_decode($myArr)); ?>
Posted by: Guest on September-15-2021
2

json_encode json_decode php examples

json_encode used when PHP retrieve data and convert Array() to [] !!!!
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
echo json_encode($arr);
//output
{"a":1,"b":2,"c":3,"d":4,"e":5}
access from js file data.a, data.b,data.c...
----------------------------------------------
  $json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
json_decode($json, true); //true turns object to associative array;

//output
array(5) {
    ["a"] => int(1)
    ["b"] => int(2)
    ["c"] => int(3)
    ["d"] => int(4)
    ["e"] => int(5)
}
Posted by: Guest on July-26-2020

Browse Popular Code Answers by Language