Json Advice

I just hit a roadblock…hoping someone has some ideas.

my data returned in json format looks like this…




"response": {

        "status": "OK",

        "content-categories": [

            {

                "id": 1,

                "name": "Pets & Animals",

                "description": null,

                "is_system": true,

                "parent_category": null,

                "type": "standard",

                "code": null,

                "last_modified": "2010-10-22 18:58:13"

            },

....




I can process most jsons just fine but this one is unique.

It seems "content-categories" really messes up Yii.




if ($api == "content-category")

	 {

		for ($i=0 ; $i<$limit; $i++) {

			$cc = new ContentCategory;			

			$cc->id = $obj->response->content-categories[$i]->id;

			$cc->category = $obj->response->content-categories[$i]->name;

			$cc->description = $obj->response->content-categories[$i]->description;

			$cc->is_system = $obj->response->content-categories[$i]->is_system;

			$cc->parent_id = $obj->response->content-categories[$i]->parent_category[0]->id;

			$cc->type = $obj->response->content-categories[$i]->type;

			$cc->code = $obj->response->content-categories[$i]->code;

			$cc->last_modified = $obj->response->content-categories[$i]->last_modified;


			$cc->save(false);

		}


	 }



I tried to make the $obj reference dynamic in my controller but that didn’t work either. “$api” substituted for “content-categories”




if ($api == "content-category")

	 {

		for ($i=0 ; $i<$limit; $i++) {

			$cc = new ContentCategory;			

			$cc->id = $obj->response->$api[$i]->id;

			$cc->category = $obj->response->$api[$i]->name;

			...

			$cc->save(false);

		}


	 }



With the existance of references to the dash "-" it causes the Yii controller to error out when calling anything (in this case another view that loads up a control panel to call the controller function)

$cc->id = $obj->response->content-categories[$i]->id;

Anyone have advice on how to deal with this? I can’t control the JSON output, the object will have a dash in this case.

In another case it does not, they used "_" underscore, which worked fine.

Is there a way to remap it ? For some reason this breaks the Yii controller.

I have tried ‘content-categories’ and “content-categories” and making it a variable.

I tried a constant but that did not work either…it expects a class

$cc->id = $obj->response->CCC[$i]->id;

PHP Error: Undefined property: stdClass::$CCC

Also tried

if ($api == "content-category")

  {


     for (&#036;i=0 ; &#036;i&lt;&#036;limit; &#036;i++) {


         &#036;cc = new ContentCategory; 


         &#036;cc-&gt;id = &#036;obj-&gt;response-&gt;&#036;this-&gt;&#036;api[&#036;i]-&gt;id;

}

But received this error:

Recoverable Error:

Object of class AppnexusController could not be converted to string

So maybe I am close!

Dear Friend

Would you please check whether the following is helpful?




$jsonString='{"status":"ok","student-profiles":[

	{"name":"jack","age":10,"sex":"male"},

	{"name":"jill","age":8,"sex":"female"},

	{"name":"jhon","age":4,"sex":"male"},

	{"name":"joy-varghese","age":4,"sex":"female"}

]}';


$jsonString=strtr($jsonString,array("-"=>"_"));

$phpObject=json_decode($jsonString);



Now




echo $phpObject->student_profiles[3]->name;// will ouput joy_varghese



"-" is not allowed in naming variables,classes or properties in PHP.

Regards.

Thank you my friend ! As always, you seem to have a great answer ! For a man in the medicine field you are technically savvy!

I also found that php seems to allow the use of curly braces $obj->{‘field-name’} works as well.

Thanks again !