0

Json String

I need to iterate the String and print whole data.

I m getting illegal offset while printing it.##

$string = '{  
       "ConditionArray":[  
          {  
             "condition_image":"\/storage\/emulated\/0\/androidlav\/470-twitter.jpg (image\/jpg)",
             "condition_name":"Maleria",
             "profile_id":"49"
          },
          {  
             "condition_image":"\/storage\/emulated\/0\/androidlav\/470-twitter.jpg (image\/jpg)",
             "condition_name":"Maleria",
             "profile_id":"49"
          }
       ],

    "AllergiesArray":[  
          {  
             "condition_image":"\/storage\/emulated\/0\/androidlav\/470-twitter.jpg (image\/jpg)",
             "condition_name":"exapample",
             "profile_id":"50"
          },

       ]
    }';
7
  • show your error code ! Commented Aug 8, 2016 at 7:25
  • use json_decode function to decode and convert it into php array then iterate Commented Aug 8, 2016 at 7:25
  • using code: $json = json_encode($string, true); print $json['ConditionArray']['condition_image']; Commented Aug 8, 2016 at 7:26
  • Warning: Illegal string offset 'ConditionArray' in D:\xampp\htdocs\noorisys\aawlascope\ws\test.php on line 43 Commented Aug 8, 2016 at 7:27
  • while using json_decode it is showing blank screen. Commented Aug 8, 2016 at 7:28

2 Answers 2

1

You just have extra comma in last line of AllergiesArray array

 "AllergiesArray":[  
          {  
             "condition_image":"\/storage\/emulated\/0\/androidlav\/470-twitter.jpg (image\/jpg)",
             "condition_name":"exapample",
             "profile_id":"50"
          }, <--- remove this comma
    ]

$json =  (array)json_decode($string);  //convert array
print $json['ConditionArray'][0]->condition_image; 
Sign up to request clarification or add additional context in comments.

Comments

0

I have found the error in you json string it's not properly encoded

Invaild json object

$string = '{
    "ConditionArray": [{
        "condition_image": "\/storage\/emulated\/0\/androidlav\/470-twitter.jpg (image\/jpg)",
        "condition_name": "Maleria",
        "profile_id": "49"
    }, {
        "condition_image": "\/storage\/emulated\/0\/androidlav\/470-twitter.jpg (image\/jpg)",
        "condition_name": "Maleria",
        "profile_id": "49"
    }],

    "AllergiesArray": [{
        "condition_image": "\/storage\/emulated\/0\/androidlav\/470-twitter.jpg (image\/jpg)",
        "condition_name": "exapample",
        "profile_id": "50"
    }, // => this line contain the error it has extra comma 
  ]
}';

use http://jsonlint.com/ to validate the json

Valid json object

$string = '{
    "ConditionArray": [{
        "condition_image": "\/storage\/emulated\/0\/androidlav\/470-twitter.jpg (image\/jpg)",
        "condition_name": "Maleria",
        "profile_id": "49"
    }, {
        "condition_image": "\/storage\/emulated\/0\/androidlav\/470-twitter.jpg (image\/jpg)",
        "condition_name": "Maleria",
        "profile_id": "49"
    }],

    "AllergiesArray": [{
        "condition_image": "\/storage\/emulated\/0\/androidlav\/470-twitter.jpg (image\/jpg)",
        "condition_name": "exapample",
        "profile_id": "50"
    }
  ]
}';

var_dump(json_decode($string));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.