0
{
    "general": {
        "knx_Gateway": "te",
        "knx_Port": "gas",
        "knx_Medium": "TP1",
        "knx_timezone": "+3"
    },
    "devices":[
      {
        "id": "0",
        "knx_dn": "testing",
        "knx_ga": "2",
        "knx_dt": "Bit"
      },
      {
        "id": "1",
        "knx_dn": "0",
        "knx_ga": "0",
        "knx_dt": "Bit"
      },
      {
        "id": "2",
        "knx_dn": "0",
        "knx_ga": "5",
        "knx_dt": "Byte"
      },
      {
        "id": "3",
        "knx_dn": "0",
        "knx_ga": "0",
        "knx_dt": "Byte"
      },
      {
        "id": "4",
        "knx_dn": "0",
        "knx_ga": "0",
        "knx_dt": "Byte"
      }
  ]
}

This is what i tried so far:

//getting the data from the HTML page into an JSON format
$knxGenSet->general->knx_Gateway = $_POST["ipgate"];
$knxGenSet->general->knx_Port = $_POST["ipport"];
$knxGenSet->general->knx_Medium = $_POST["ipmedium"];
$knxGenSet->general->knx_timezone = $_POST["iptimezone"];

for($i=0; $i<5; $i++){
    echo "The number is " . $i . "<br>";

    $knxGenSet->devices->id = $i;
    $knxGenSet->devices->knx_ga = $_POST["dn" + $i];
    $knxGenSet->devices->knx_dn = $_POST["ga" + $i];
    $knxGenSet->devices->knx_dt = $_POST["dt" + $i];
}

I cant get the other part with devices right it seems that I am doing something wrong here btw i can allready read this json format .. the reading part works perfectly! :)

Thanks in advance (kind of newb on json territory! :()

2

1 Answer 1

1

The format can be specified with an object:

<?php
$_POST = [
    "ipgate" => "testing",
    "ipport" => "testing",
    "ipmedium" => "testing",
    "iptimezone" => "testing",
    "dn0" => "test_1",
    "ga0" => "test_1",
    "dt0" => "test_1",
    "dn1" => "test_2",
    "ga1" => "test_2",
    "dt1" => "test_2",
];

$knxGenSet = (object) [
    'general' => (object) [],
    'devices' => []
];

$knxGenSet->general->knx_Gateway = $_POST["ipgate"];
$knxGenSet->general->knx_Port = $_POST["ipport"];
$knxGenSet->general->knx_Medium = $_POST["ipmedium"];
$knxGenSet->general->knx_timezone = $_POST["iptimezone"];

for($i=0; $i<2; $i++){
  //  echo "The number is " . $i . "<br>";

    $knxGenSet->devices[] = (object) [
        'id' => $i,
        'knx_ga' => $_POST["dn" . $i],
        'knx_dn' => $_POST["ga" . $i],
        'knx_dt' => $_POST["dt" . $i]
    ];
}

echo json_encode($knxGenSet);

result:

{
  "general": {
    "knx_Gateway": "testing",
    "knx_Port": "testing",
    "knx_Medium": "testing",
    "knx_timezone": "testing"
  },
  "devices": [
    {
      "id": 0,
      "knx_ga": "test_1",
      "knx_dn": "test_1",
      "knx_dt": "test_1"
    },
    {
      "id": 1,
      "knx_ga": "test_2",
      "knx_dn": "test_2",
      "knx_dt": "test_2"
    }
  ]
}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.