Revathy Santhanam
Revathy Santhanam

Reputation: 15

PHP json_decode() with multi level key-value pairs from C#

I am using multiple levels of JSON data coming into php from a C# application, as in:

return new RootObject()
{
ID_Project = 4,
Name_Project = "Test",
Receiver_ID = 4,
Receiver_Name = "ABCDE",
UserID = 20,
UserRole = new User_Role()
{
ID_User = 20,
Role_User = "level 3",
User_Role_Description = "U",
UserGroup = new List<user_group>()
{
new User_Group() { ID_UserGroup = 30, Name_UserGroup = "usergroup8", UserID = 20 },
new User_Group() { ID_UserGroup = 31, Name_UserGroup = "usergroup9", UserID = 21 },
new User_Group() { ID_UserGroup = 32, Name_UserGroup = "usergroup10", UserID = 22 }
}
}
};

i am having troubles accessing the second level: UserRole and 3rd level: User_Group in my php script. im currently trying like this:

foreach ($phpArray as $key => $value) {
echo "<h2>$key</h2>";
foreach ($value as $k => $v) {

echo "$k | $v
";

foreach ($v as $key1) {

echo "$key1
";
}
}
}

new version in PHP: (after edit)

     //connect to the database
    $con = mysqli_connect("localhost","userid","password","database");
    if (mysqli_connect_errno()) {
        echo 'Failed to connect to MySQL: ' . mysqli_connect_error();
        exit();
    };
    $jsonData = file_get_contents("JSON_File.json");
    $phpArray = json_decode($jsonData);

    $userRole = $phpArray->UserRole;
    $userGroups = $phpArray->UserRole->UserGroup;
    $id_project=$phpArray->ID_Project;
    $name_project=$phpArray->Name_Project;
$id_receiver=$phpArray->Receiver_ID;
$name_receiver=$phpArray->Receiver_Name;
$userid=$phpArray->UserID;
$id_user=$phpArray->UserRole->ID_User;
$role_user=$phpArray->UserRole->Role_User;
$role_user_description=$phpArray->UserRole->User_Role_Description;

    $sqlStr = "INSERT INTO project (`ID_Project`,`Name_Project`,`Receiver_ID`,`Receiver_Name`,`UserID`) VALUES ($id_project, '$name_project',$id_receiver,'$name_receiver',$userid);" ;


    $sqlStr.= "INSERT INTO user_role (`ID_User`,`UserRole`,`User_Role_Description`)  VALUES ($id_user,'$role_user','$role_user_description');" ;
    foreach($userGroups as $userGroup) {
    $id_usergroup=$userGroup->ID_UserGroup;
    $name_usergroup=$userGroup->Name_UserGroup . PHP_EOL;
    $userid=$userGroup->UserID . PHP_EOL;  

    $sqlStr.= "INSERT INTO user_group (`ID_UserGroup`,`Name_UserGroup`,`UserID`) VALUES ($id_usergroup, '$name_usergroup',$userid)" ;
    }
    if (mysqli_multi_query($con, $sqlStr)) {
    do {
        /* store first result set */
        if ($result = mysqli_store_result($con)) {
            while ($row = mysqli_fetch_row($result)) {
                printf("%s\n", $row[0]);
            }
            //check the outcome

        /* print divider */
        if (mysqli_more_results($con)) {
            printf("-----------------\n");
        }
    } while (mysqli_next_result($con));
    }

EDIT: I have an issue with accessing the 3rd level data elements. For Eg., I am able to get the values uptil Project-> Stand_Orte-> Modul. but after that Project-> Stand_Orte-> Modul-> MessKanal Throws an error in PHP as " Trying to get property of non-object "

the JSON class structure from C# is as follows:

public class User_Group
    {
        public int ID_UserGroup { get; set; }
        public string Name_UserGroup { get; set; }
        public int UserID { get; set; }
    }
    public class User_Role
    {
        public int ID_User { get; set; }
        public string Role_User { get; set; }
        public string User_Role_Description { get; set; }
        public List<User_Group> UserGroup { get; set; }
    }

    public class Stand_Orte
    {
        public int ID { get; set; }
        public string Bezeichnung { get; set; }
        public List<Modul> modul { get; set; }
    }

    public class Modul
    {
        public string ID { get; set; }
        public string Seriennummer { get; set; }
        public string Bezeichnung { get; set; }
        public string StandortID { get; set; }
        public List<Mess_Kanal> MessKanal { get; set; }
    }

    public class Mess_Kanal
    {
        public string ID { get; set; }
        public string ModulID { get; set; }
        public List<LogMess_Daten> LogMessDaten { get; set; }
    }

    public class LogMess_Daten
    {
        public string KanalID { get; set; }
        public string Zeitstempel { get; set; }

    }


    public class RootObject
    {
        public int ID_Project { get; set; }
        public string Name_Project { get; set; }
        public int Receiver_ID { get; set; }
        public string Receiver_Name { get; set; }
        public int UserID { get; set; }
        public User_Role UserRole { get; set; }
        public Stand_Orte Standorte { get; set; }
    }

does anyone have an idea?

Thanks, Revathy

Upvotes: 0

Views: 482

Answers (1)

RichardBernards
RichardBernards

Reputation: 3097

$jsonData = file_get_contents("JSON_superproject.json");
$json = json_decode($jsonData);

$userRole = $json->UserRole;
$userGroups = $json->UserRole->UserGroup;

So for example when trying to read the usergroups:

foreach($userGroups as $userGroup) {
    echo $userGroup->Name_UserGroup . PHP_EOL;
}

Since you are using everything as an object now, to load pieces which are between {}, you can get to them by using the -> in PHP, pieces enclosed by [] are still arrays and what you are used to.

Examples:

echo $json->ID_Project;
echo $json->UserRole->ID_User;

Extra SQL information for the usergroups: Replace your existing line with the sql statement to the following:

foreach($userGroups as $userGroup) {
    $sqlStr .= "INSERT INTO user_group (`ID_UserGroup`,`Name_UserGroup`,`UserID`) VALUES (" . $userGroup->ID_UserGroup . ", '" . $userGroup->Name_UserGroup . "', " . $userGroup->UserID . ");";
}

Upvotes: 1

Related Questions