0

In PHP I am trying to put together a JSON string that gets returned with the status of the takes and any errors that may have occurred:

public function create() {
    $data = Validate::sanitize($_POST['data']);
    parse_str($data);

    // Something

    $this->JSONResponse[] = $this->addCost($shipmentId, $cost);

    $this->JSONResponse[] = '{"code":"0", "type":"info", msg":"Shipment created successfully."}';
    return '{"response":['.json_encode($this->JSONResponse).']}';
}

public function addCost($shipmentId, $cost) {
    if ($cost > 0) {
        // Something
    } else {
        return '{"code":"1", "type":"info", msg":"Cost not added as it was 0 or left out."}';
    }
}

The ways I have tried including the one in the above example don't work. I either get a string that isn't JSON or a JSON object consisting of indexes containing the raw JSON string.

How can I get this to output what I want?

1
  • in both your cases you are creating msg", this should be "msg". All json properties must be enclosed by quotes unless they are numbers. Commented Mar 13, 2014 at 10:57

2 Answers 2

1

Is there a reason why you're writing the JSON string manually instead of building a PHP array and taking advantage of the json_encode function?

If not you should use

public function addCost($shipmentId, $cost) {
    if ($cost > 0) {
        // Something
    } else {
        return json_encode(array("code"=>1, "type"=>"info", "msg"=>"message"));
    }
}

And as Kami said, ther's a typo here :

'{"code":"0", "type":"info", msg":"Shipment created successfully."}';

It lacks a "

'{"code":"0", "type":"info", "msg":"Shipment created successfully."}';
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, didn't see the typo but building it as an array instead of a string is much nicer and it works now.
1

Don't create the string by hand - create a PHP object or array - and then use json_encode to create a string out of it. Something like this:

public function create() {
    $data = Validate::sanitize($_POST['data']);
    parse_str($data);

    // Something

    $this->JSONResponse[] = $this->addCost($shipmentId, $cost);

    $this->JSONResponse[] = json_encode(array("code" => 0,
                                              "type" => "info",
                                              "msg"  => "Shipment created successfully."));
    return json_encode(array("response" => $this->JSONResponse[]));
}

public function addCost($shipmentId, $cost) {
    if ($cost > 0) {
        // Something
    } else {
        return array("code" => 1,
                     "type" => "info"
                     "msg"  => "Cost not added as it was 0 or left out.");
    }
}

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.