且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

发布阅读阅读json标签在php

更新时间:2023-12-04 13:32:28

颜色的标记名称为rvalue,gvalue和bvalue,但您使用的是rValue,gValue和bValue。我认为这是您的代码中的问题。

  $ imageId = 1; 
$ a [username] =abc@abc.com;
$ a [password] =abc @ 123;
$ a [access_key] =api_key;
$ a [description] =***文本;
$ a [ocassion] =1;
$ a [brands] [0] [brandName] =李;
$ a [brands] [0] [xcoord] =1345;
$ a [brands] [0] [ycoord] =2345;
$ a [brands] [0] [color] [colorId] =8;
$ a [brands] [0] [color] [rvalue] =234;
$ a [brands] [0] [color] [gvalue] =213;
$ a [brands] [0] [color] [bvalue] =432;
$ a [brands] [1] [brandName] =李;
$ a [brands] [1] [xcoord] =1345;
$ a [brands] [1] [ycoord] =2345;
$ a [brands] [1] [color] [colorId] =8;
$ a [brands] [1] [color] [rvalue] =234;
$ a [brands] [1] [color] [gvalue] =213;
$ a [brands] [1] [color] [bvalue] =432;

$ json = json_decode(json_encode($ a));
$ brandTagsArr = $ json-> brands;

foreach($ brandTagsArr as $ brandTag){
// print_r($ brandTag-> color); exit;
$ brandName = $ brandTag-> brandName; //需要获取名称并关联品牌标签id
$ xCoord = $ brandTag-> xcoord; //
$ yCoord = $ brandTag-> ycoord;
// $ this-> rest_image_upload_model-> insertBrandTags($ imageId,$ brandName,$ xCoord,$ yCoord);
echo $ imageId。===>$ brandName。===>。$ xCoord。===>。$ yCoord。< br>;
//插入颜色
echoinsert brand tags< br>;
// $ color = $ brandTag ['color']; // returns nothing FAILS
$ color = $ brandTag-> color; // returns nothing FAILS

echocolor id => 。 $ color-> colorId;
echo $ imageId。===>。$ color-> colorId。===>。$ color-> rvalue。===>。$ color-&gt ; gvalue。===>。$ color-> bvalue。< br>;
echoinsert color tags< br>;
//结束插入颜色
}

数组编码和解码只有。希望它有帮助。


I am sending the following json to the server

{
"username":"abc@abc.com"    ,
"password":"abc@123","access_key": "api_key",
    "brands": [
        {  "brandname": "Lee","xcoord": "1345",
            "ycoord": "2345","color": {"colorId":8, "rvalue": "234",
                "gvalue": "213","bvalue": "233" }
        },
        {   "brandname": "Pepe","xcoord": "432",
            "ycoord": "4210","color": {"colorId":5, "rvalue": "234",
                "gvalue": "213","bvalue": "233"}
        }
    ],
    "description": "free text",
    "ocassion": 1, // an ocassion id goes here.
    "other_tags": ["other1","other2"],
    "upload_platform":"android|iOS|web"
}

When i try to read a specific object color, which resides brands array object as below I am unable to do so and the echo fails, printing nothing. I have never written php, its so easy in java to just use gson and define models that would fill every model up.

$userData = urldecode ( $_POST['form'] );
$json = json_decode ( $userData );
$brandTagsArr = $json->brands;

foreach ($brandTagsArr as $brandTag){
                $brandName = $brandTag->brandName; // need to fetch the name and associate brand tag id
                $xCoord = $brandTag->xcoord; // 
                $yCoord = $brandTag->ycoord;
                $this->rest_image_upload_model->insertBrandTags($imageId, $brandName, $xCoord, $yCoord);
                // insert colors
                echo  "insert brand tags <br>";
                $color = $brandTag['color']; // returns nothing FAILS
                $color = $brandTag->color; // returns nothing FAILS
                echo "color id" . $color['colorId'];
                $this->rest_image_upload_model->insertColorTag($imageId, $color['colorId'],$color['rValue'], $color['gValue'], $color['bValue']);
                echo "insert color tags<br>";
                // end inserting colors
            }

the tag name for colors is rvalue,gvalue and bvalue, but you are using as rValue,gValue and bValue. I think thats the issue in your code.

    $imageId = 1;
    $a["username"] = "abc@abc.com";
    $a["password"] = "abc@123";
    $a["access_key"] = "api_key";
    $a["description"] = "free text";
    $a["ocassion"] = "1";
    $a["brands"][0]["brandName"] = "Lee";
    $a["brands"][0]["xcoord"] = "1345";
    $a["brands"][0]["ycoord"] = "2345";
    $a["brands"][0]["color"]["colorId"] = "8";
    $a["brands"][0]["color"]["rvalue"] = "234";
    $a["brands"][0]["color"]["gvalue"] = "213";
    $a["brands"][0]["color"]["bvalue"] = "432";
    $a["brands"][1]["brandName"] = "Lee";
    $a["brands"][1]["xcoord"] = "1345";
    $a["brands"][1]["ycoord"] = "2345";
    $a["brands"][1]["color"]["colorId"] = "8";
    $a["brands"][1]["color"]["rvalue"] = "234";
    $a["brands"][1]["color"]["gvalue"] = "213";
    $a["brands"][1]["color"]["bvalue"] = "432";

    $json = json_decode(json_encode($a));
    $brandTagsArr = $json->brands;

    foreach ($brandTagsArr as $brandTag) {
//            print_r($brandTag->color);exit;
            $brandName = $brandTag->brandName; // need to fetch the name and associate brand tag id
            $xCoord = $brandTag->xcoord; // 
            $yCoord = $brandTag->ycoord;
//            $this->rest_image_upload_model->insertBrandTags($imageId, $brandName, $xCoord, $yCoord);
             echo $imageId."===>".$brandName."===>".$xCoord."===>".$yCoord."<br>";
        // insert colors
        echo "insert brand tags <br>";
//            $color = $brandTag['color']; // returns nothing FAILS
            $color = $brandTag->color; // returns nothing FAILS

            echo "color id =>" . $color->colorId;
            echo $imageId."===>".$color->colorId."===>".$color->rvalue."===>".$color->gvalue."===>".$color->bvalue."<br>";
            echo "insert color tags<br>";
            // end inserting colors
        }

For your convenience i ve created an array encoded and decoded there only.Hope it helps.