发布阅读阅读json标签在php [英] Issue reading reading json tag in php
问题描述
我将以下json发送到服务器
{
/ pre>
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 colorId:5,rvalue:234,
gvalue:213,bvalue:233}
}
],
description:free text,
ocassion:1,//一个ocassion id在这里。
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; //需要获取名称并关联品牌标签id
$ xCoord = $ brandTag-> xcoord; //
$ yCoord = $ brandTag-> ycoord;
$ this-> rest_image_upload_model-> insertBrandTags($ imageId,$ brandName,$ xCoord,$ yCoord);
//插入颜色
echoinsert brand tags< br>;
$ color = $ brandTag ['color']; // returns nothing FAILS
$ color = $ brandTag-> color; // returns nothing FAILS
echocolor id。 $ color ['colorId'];
$ this-> rest_image_upload_model-> insertColorTag($ imageId,$ color ['colorId'],$ color ['rValue'],$ color ['gValue'],$ color ['bValue']) ;
echoinsert color tags< br>;
// end inserted colors
}
解决方案颜色的标记名称为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-> ; 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.
这篇关于发布阅读阅读json标签在php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!