Viewed   66 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":"[email protected]"}';  

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

 Answers

5

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

Outputs:

Array
(
    [id] => 1
    [name] => foo
    [email] => [email protected]
)
Monday, August 29, 2022
5

Try something like this:

//initialize array
$myArray = array();

//set up the nested associative arrays using literal array notation
$firstArray = array("id" => 1, "data" => 45);
$secondArray = array("id" => 3, "data" => 54);

//push items onto main array with bracket notation (this will result in numbered indexes)
$myArray[] = $firstArray;
$myArray[] = $secondArray;

//convert to json
$json = json_encode($myArray);
Friday, December 23, 2022
1

Typescript is (a superset of) javascript, so you just use JSON.parse as you would in javascript:

let obj = JSON.parse(jsonString);

Only that in typescript you can have a type to the resulting object:

interface MyObj {
    myString: string;
    myNumber: number;
}

let obj: MyObj = JSON.parse('{ "myString": "string", "myNumber": 4 }');
console.log(obj.myString);
console.log(obj.myNumber);

(code in playground)

Friday, December 2, 2022
 
tayfun
 
4

you can use it like this, in JSON format when you evaluate false value it will give you blank, and when you evaluate true it will give you 1.

$str = '[{"clientId":"17295c59-4373-655a-1141-994aec1779dc","channel":"/meta/connect","connectionType":"long-polling","ext":{"fm.ack":false,"fm.sessionId":"22b0bdcf-4a35-62fc-3764-db4caeece44b"},"id":"5"}]';

$arr = json_decode($str,true);

if($arr[0]['ext']['fm.ack'])    // suggested by **mario**
{
    echo "true";    
}
else {
    echo "false";   
}
Thursday, October 6, 2022
4

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

-(instancetype) initWithParameters:(NSDictionary*)parameters
{
    self = [super init];
    if (self) {
        //initializations
        _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 
                                                        options:0 
                                                          error:&localError];

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];
Wednesday, August 31, 2022
 
hardest
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :