0

I am trying to parse this JSON CURL response. I have tried json_encode, json_decode, serialize, unserialize. This is the raw response from CURL:

    {
    d: "[{"BranchName":"D7","BranchCode":"D7","Available":"0","StorageOnlySalesCenterMsg":null,"IsRestricted":false},
         {"BranchName":"A4","BranchCode":"A4","Available":"0","StorageOnlySalesCenterMsg":null,"IsRestricted":false},
         {"BranchName":"525A","BranchCode":"525A","Available":"0","StorageOnlySalesCenterMsg":null,"IsRestricted":false}]"
    }

I would like to just do a simple foreach statement like this:

foreach($json as $data){
    echo 'Branch: ' . $data['BranchName'] . ', ';
}

Result:

Branch: D7,
Branch: A4,
Branch: 525A,

Any suggestions?

1
  • 3
    The JSON is invalid. It looks like an attempted JSON string inside of another JSON string, but since the inside quotes are not escaped, it's breaking the format. Talk to whoever you're getting the response from, and have them fix it. Commented Jul 22, 2021 at 19:46

1 Answer 1

1

As aynber already pointed out, this would be a valid representation of your json object:

{
"d": [{
        "BranchName": "D7",
        "BranchCode": "D7",
        "Available": "0",
        "StorageOnlySalesCenterMsg": null,
        "IsRestricted": false
    },
    {
        "BranchName": "A4",
        "BranchCode": "A4",
        "Available": "0",
        "StorageOnlySalesCenterMsg": null,
        "IsRestricted": false
    },
    {
        "BranchName": "525A",
        "BranchCode": "525A",
        "Available": "0",
        "StorageOnlySalesCenterMsg": null,
        "IsRestricted": false
    }
]
}
Sign up to request clarification or add additional context in comments.

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.