0

I am requesting my output look like this:

Response
{
    error_num: 0
    error_details:
    [
        {
            "zipcode": 98119
        },
        {
            "zipcode": 98101
        }
    ]
}

The values are irrelevant for this example.

My code looks like this:

$returndata = array('error_num' => $error_code);
$returndata['error_details'] = $error_msg;

$temp_data = array();
$temp_value = '';
foreach ($zipcodes_array as $value) {
    //$temp_data['zipcode'] = $value;
    //$temp_value .= json_encode($temp_data);
    $temp_value .= '{"zipcode":$value},';
}
//$returndata['test'] = $temp_value;
$returndata['zipcodes'] = $temp_value;
echo json_encode($returndata);

My output varies depending on my different attempts (which you can see with the commented out things) but basically, I don't understand how the 3rd part (the part with the zipcodes) doesn't have a key or a definition before the first open bracket "["

Here is the output for the code above:

{"error_num":0,"error_details":"","zipcodes":"{\"zipcode\":11111},{\"zipcode\":11112},{\"zipcode\":11113},{\"zipcode\":22222},{\"zipcode\":33333},{\"zipcode\":77325},{\"zipcode\":77338},{\"zipcode\":77339},{\"zipcode\":77345},{\"zipcode\":77346},{\"zipcode\":77347},{\"zipcode\":77396},{\"zipcode\":81501},{\"zipcode\":81502},{\"zipcode\":81503},{\"zipcode\":81504},{\"zipcode\":81505},{\"zipcode\":81506},{\"zipcode\":81507},{\"zipcode\":81508},{\"zipcode\":81509},"}

Obviously i did not show the variables being filled/created because its through MySQL. The values are irrelevant. Its the format of the output i'm trying to get down. I don't understand how they have the "zipcode": part in between {} brackets inside another section which appears to be using JSON_ENCODE

It is close but see how it still has the "zipcodes": part in there defining what key those values are on? My question is, is the "response" above being requested by a partner actually in JSON_ENCODE format?? or is it in some custom format which I'll just have to make w/out using any json features of PHP? I can easily write that but based on the way it looks in the example above (the response) I was thinking it was JSON_ENCODE being used.

Also, it keeps putting the \'s in front of the " which isn't right either. I know it's probably doing that because i'm json_encode'ing a string. Hopefully you see what i'm trying to do.

If this is just custom stuff made to resemble JSON, I apologize. I've tried to ask the partner but I guess i'm not asking the right questions (or the right person). They can never seem to give me answers.

EDIT: notice my output also doesn't have any [ or ] in it. but some of my test JSON_ENCODE stuff has had those in it. I'm sure its just me failing here i just cant figure it out.

DerekConlon
  • 353
  • 1
  • 5
  • 17
  • 1
    if you dont have \ data then var finalData = str.replace(/\\/g, ""); or stripslashes($str); – Josua Marcel C Nov 10 '14 at 01:12
  • Good Idea. I'll do that but the format of the requested output still doesn't match mine and that is a bigger concern than the slashes. I keep thinking its some kind of json_encode inside another json_encode but I just can't figure it out. – DerekConlon Nov 10 '14 at 01:17
  • 1
    It just looks like nested JSON to me although incorrectly formatted, look at this question. http://stackoverflow.com/questions/15810257/create-nested-json-object-in-php – EternalHour Nov 10 '14 at 01:20
  • Thank you for that link. That's what I was thinking it was, but if its not the same format (and its not) that means its probably something custom, or not JSON at all. I think i'll just write something that makes the output do what they want and then talk to them about it after the fact if it doesn't work because they were expecting JSON or something like that. I appreciate the answers. – DerekConlon Nov 10 '14 at 01:25

2 Answers2

1

If you want your output to look like the JSON output at the very top of your question, write the code like this:

$returndata = array('error_num' => $error_code);
$returndata['error_details'] = array();

foreach ($zipcodes_array as $value) {
    $returndata['error_details'][] = array('zipcode' => (int)$value);
}
echo 'Response ' . json_encode($returndata);

This will return JSON formatted like you requested above.

Response
{
    error_num: 0
    error_details:
    [
        {
             "zipcode": 98119
        },
        {
             "zipcode": 98101
        }
    ]
}
Manmaru
  • 578
  • 3
  • 8
1

Can you just use single ZipCode object as associative array, push all your small ZipCode objects to the ZipCodes array and encode whole data structure. Here is the code example:

$returndata = array(
    'error_num'     => $error_code,
    'error_details' => array()
);

foreach ($zipcodes_array as $value) {
    $returndata['error_details'][] = array('zipcode' => $value);
}
echo json_encode($returndata);
  • This one is really close also but there are differences, such as the word "zipcodes" in front of the array of zipcodes. I really think the example provided to me (which is copied at the top of my post) is mistaken. there doesn't seem to be a way (with json) to duplicate it. but your example is way closer than mine was. – DerekConlon Nov 10 '14 at 01:38
  • 1
    I removed 'zipcodes' from the result array and now my answer is the same as Manmaru's answer :) He was first. – dobriy_santa Nov 10 '14 at 01:55
  • I appreciate you helping also :) – DerekConlon Nov 10 '14 at 02:03