在PHP

时间:2015-08-19 21:41:40

标签: php json codeigniter

我将以下json发送到服务器

{
"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"
}

当我尝试读取特定对象颜色时,如下所示驻留品牌数组对象我无法这样做并且回显失败,什么都不打印。我从来没有写过php,它在java中很容易使用gson并定义可以填满每个模型的模型。

$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
            }

1 个答案:

答案 0 :(得分:1)

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

    $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
        }

为了您的方便,我创建了一个仅在那里编码和解码的数组。希望它有所帮助。