Asked  7 Months ago    Answers:  5   Viewed   33 times

I am trying to parse a string in JSON, but not sure how to go about this. This is an example of the string I am trying to parse into a PHP array.

$json = '{"id":1,"name":"foo","email":""}';  

Is there some library that can take the id, name, and email and put it into an array?



It can be done with json_decode(), be sure to set the second argument to true because you want an array rather than an object.

$array = json_decode($json, true); // decode json


    [id] => 1
    [name] => foo
    [email] =>
Wednesday, March 31, 2021
answered 7 Months ago

What a HORRENDOUS debug session.. well there's good news.. I figured it out..

I started looking at it using AJAX and logging it with Firebug... and it turns out json_decode (or eval by the way) cannot handle ", which is what PHPUnit sends back (Come on Sebastian!), so to fix it:

$json = str_replace('"', '"', $json);

Now I thought they were the same.. maybe someone can enlighten me..

Wednesday, March 31, 2021
answered 7 Months ago

The current answers contain a lot of hand-rolled or library code. This is not necessary.

  1. Use JSON.parse('{"a":1}') to create a plain object.

  2. Use one of the standardized functions to set the prototype:

    • Object.assign(new Foo, { a: 1 })
    • Object.setPrototypeOf({ a: 1 }, Foo.prototype)
Friday, June 4, 2021
answered 5 Months ago

I would suggest to implement an init method for your Restaurant class.

-(instancetype) initWithParameters:(NSDictionary*)parameters
    self = [super init];
    if (self) {
        _validationCode = parameters[@"validationCode"]; // may be NSNull
        _firstName = [parameters[@"FirstName"] isKindOfClass:[NSNull class]] ? @"" 
                     : parameters[@"FirstName"];
    return self;

Note: the fact that you may have JSON Nulls, makes your initialization a bit elaborate. You need to decide how you want to initialize a property, when the corresponding JSON value is Null.

Your parameters dictionary will be the first level dictionary from the JSON Array which you got from the server.

First, create a JSON representation, that is a NSArray object from the JSON:

NSError* localError;
id restaurantsObjects = [NSJSONSerialization JSONObjectWithData:data 

IFF this did not fail, your restaurantsObjects should now be an NSArray object containing the restaurants as NSDictionarys.

Now, it will be straight forward to create a NSMutableArray which will be populated with Restaurant objects:

NSMutableArray* restaurants = [[NSMutableArray alloc] init];
for (NSDictionary* restaurantParameters in restaurantsObjects) {
    Restaurant* restaurant = [Restaurant alloc] initWithParameters: restaurantParameters];
    [restaurants addObject:restaurant];

and finally, you may set a property restaurants in some controller:

self.restaurants = [restaurants copy];
Tuesday, October 12, 2021
answered 2 Weeks ago

Debugging suggestion:

Check the output of json_last_error(). It should give you an exact reason why it doesn't work. Available from PHP 5.3.0 only, though.

The reason:

JSONP is not identical with JSON. It contains extra data that breaks json_decode().


Remove the extra brackets using substr($AVDecode, 1, strlen($AVDecode)-2)

Friday, October 22, 2021
answered 3 Days ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :