6

I want to know how can I create object like below example. Please help me to create object. I searched in google but I didn't get what I want. I'm noob.

{
    "ERROR_CODE" = 0;
    "M_USER" = {
        "CREATE_DATE" = "2018-05-09 13:57:49";
        "CREATE_USER_ID" = t1074567;
        "FACE_PICTURE_FILE_PATH" = "<null>";
        "MAIL_ADDRESS" = "Testing@gmail.com";
        "NATIVE_LANGUAGE_CD" = 102;
        "REQ_LANGUAGE_CD" = 102;
        "TERMINAL_ID" = "C71B456F-EA16-4734-8C9B-00B0856143DA";
        "TERMINAL_TYPE" = 1;
        "TOTAL_GRADE" = 0;
        "TRANSLATABLE_FLG" = 1;
        "UPDATE_DATE" = "2018-05-09 13:57:49";
        "UPDATE_USER_ID" = tdu1074567;
        "USER_ID" = tdu1074567;
        "USER_NAME" = Testing;
        "USER_PWD" = testing123;
        "VALID_FLG" = 1;
    };
    "TRANS_LANGUAGE" = (
        {
            C = 102;
            L = 10203;
        },
        {
            C = 101;
            L = 10101;
        }
    );
}  
Krzysztof Janiszewski
  • 3,763
  • 3
  • 18
  • 38
Kyaw Zin Wai
  • 449
  • 5
  • 10
  • 26

2 Answers2

18

Here is a solution

$newObject = new stdClass;

$newObject->ERROR_CODE = 0;
$newObject->M_USER = new stdClass;
$newObject->M_USER->CREATE_DATE = "2018-05-09 13:57:49";

And so on.

Krzysztof Janiszewski
  • 3,763
  • 3
  • 18
  • 38
1
<?php
#Just convert it into a JSON string and decode it 
$your_json =  '{
    "ERROR_CODE": 0,
    "M_USER": {
        "CREATE_DATE": "2018-05-09 13:57:49",
        "CREATE_USER_ID": "t1074567",
        "FACE_PICTURE_FILE_PATH": "<null>",
        "MAIL_ADDRESS": "Testing@gmail.com",
        "NATIVE_LANGUAGE_CD": 102,
        "REQ_LANGUAGE_CD": 102,
        "TERMINAL_ID": "C71B456F-EA16-4734-8C9B-00B0856143DA",
        "TERMINAL_TYPE": 1,
        "TOTAL_GRADE": 0,
        "TRANSLATABLE_FLG": 1,
        "UPDATE_DATE": "2018-05-09 13:57:49",
        "UPDATE_USER_ID": "tdu1074567",
        "USER_ID": "tdu1074567",
        "USER_NAME": "Testing",
        "USER_PWD": "testing123",
        "VALID_FLG": 1
    },
    "TRANS_LANGUAGE": [
        {
            "C": 102,
            "L": 10203
        },
        {
            "C": 101,
            "L": 10101
        }
    ]
}';

$object = json_decode($your_json);

print_r($object);
Melvin More
  • 60
  • 2
  • 6
  • I really think the answers with '$newObject = new stdClass' are simpler and more to-the-point in answering this question. Your conversion using a json structure, then using json_decode results in a STRING of json notation. Seems simpler just to use the stdClass. – McAuley Dec 28 '20 at 21:15
  • 1
    It's an alternative if you have POST or GET or a String and want to convert into a class. – Melvin More Jan 07 '21 at 16:15