Yii Framework Forum: Json Advice - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Json Advice Rate Topic: -----

#1 User is offline   AustinGeek 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 173
  • Joined: 22-September 12

Posted 22 February 2013 - 10:11 PM

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.
0

#2 User is offline   AustinGeek 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 173
  • Joined: 22-September 12

Posted 23 February 2013 - 12:02 PM

View PostAustinGeek, on 22 February 2013 - 10:11 PM, said:

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.
0

#3 User is offline   AustinGeek 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 173
  • Joined: 22-September 12

Posted 23 February 2013 - 12:07 PM

View PostAustinGeek, on 23 February 2013 - 12:02 PM, said:

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
0

#4 User is offline   AustinGeek 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 173
  • Joined: 22-September 12

Posted 23 February 2013 - 12:20 PM

View PostAustinGeek, on 23 February 2013 - 12:07 PM, said:

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 ($i=0 ; $i<$limit; $i++) {
$cc = new ContentCategory;
$cc->id = $obj->response->$this->$api[$i]->id;
...
}

But received this error:
Recoverable Error:
Object of class AppnexusController could not be converted to string

So maybe I am close!
0

#5 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 23 February 2013 - 02:01 PM

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.
0

#6 User is offline   AustinGeek 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 173
  • Joined: 22-September 12

Posted 23 February 2013 - 02:43 PM

View Postseenivasan, on 23 February 2013 - 02:01 PM, said:

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 !
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users