ichthyocentaurs
ichthyocentaurs

Reputation: 2175

Issue reading reading json tag in php

I am sending the following json to the server

{
"username":"[email protected]"    ,
"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
            }

Upvotes: 0

Views: 72

Answers (1)

Niranjan N Raju
Niranjan N Raju

Reputation: 11987

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"] = "[email protected]";
    $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.

Upvotes: 1

Related Questions