0

I am working with HTTP API for sending SMS. I am trying to decode this json file which i actually receive after i made a post URL. Now i want to know is how can i decode the thing.. if this is static then i can decode but the fact is it have number key which may increase to any extension. In that case what i will do and how will i decode that post and also i need to save in mysql. Please anyone help me out of this problem.

{"msg_id":"4396-7666-1337896-1dc5c765ad7-5537bb07-123638242","SenderId":"EXECUT","linecount":"1","billcredit":"0.00","message":"Hello+Test+Message","sendondate":"2015-04-22 20:45:00","seq_id":{"**1**":{"valid":"true","billcredit":"1.00","id_provider":"24","providerkey":"HUTCH","regionKey":"WB","originalnumber":"1234567890","validnumber":"1234567890","mnpID":"106","dlr_seq":1},"**2**":{"valid":"true","billcredit":"1.00","id_provider":"24","providerkey":"HUTCH","regionKey":"WB","originalnumber":"1234567890","validnumber":"1234567890","mnpID":"106","dlr_seq":2}}}

I actually did the following

$json = file_get_contents($daat);
$obj = json_decode($json); 
$num= $obj->seq_id->{'1'}->validnumber;

But this will be valid only if i am having static json what to do incase of dynamic one.

Magesh Kumaar
  • 1,485
  • 2
  • 10
  • 29
Seego
  • 1
  • 3
  • 1
    Welcome to Stack Overflow! This question is a little short on information. Can you share what you have tried, and what problems you have run into? – Jay Blanchard Apr 22 '15 at 17:36
  • I am actually working with HTTP API for sending sms. i actually use curl to post a link and receive a json string. I need to decode that json string. i am not being able to understand how to do the same. – Seego Apr 22 '15 at 17:41
  • This old PHP question? [How to parse json response from CURL](http://stackoverflow.com/questions/17016506/how-to-parse-json-response-from-curl) Have you got that far at least, and where have you got stuck? – Rup Apr 22 '15 at 17:54
  • yeah.. i get the response if i send one number.. i was stucked on sending bulk.. as it will generate json like i mention.. – Seego Apr 22 '15 at 18:03
  • `json_decode($json, TRUE);` makes it into an (associative) array. Then you can do `$num= $obj['seq_id'][1]['validnumber'];` – gen_Eric Apr 22 '15 at 18:14
  • i tried it but not working for me. @RocketHazmat . I actually can decode. for static json my problem is with dynamic json where number i.e after seq_id may increase to 10k. In that case what should i do. – Seego Apr 22 '15 at 18:16
  • Use a loop, first loop over your parent objects and in that loop you loop over the `seq_id`(array?). – GuyT Apr 22 '15 at 18:17
  • could be explain how to do it..@GuyT – Seego Apr 22 '15 at 18:19
  • ` $value){ if ($key === "seq_id"){ $temp = $json_decode[$key]; foreach ($temp as $seqKey => $seqVal){ print_r($temp[$seqKey]); } } } ?>` – GuyT Apr 22 '15 at 18:33
  • With your code i receive array now tell the way to decode the array.. But i must thank you as i can see something different that i didnt make. Well if you dont mind tell me if i want to get numbers from the array. and also to save it in db. @GuyT – Seego Apr 22 '15 at 18:45
  • @Seego just follow some tutorials. This is very basic! – GuyT Apr 22 '15 at 18:51
  • Thank you ..for your help if you could tell me how can i call array element 1 by 1 so that i can store in db. i use echo $temp[$seqKey]['validnumber']; to get array element. but it pull all the data in 1 shot. i want to use a loop so that i can save but i cant understand how to do it.. please help me. @GuyT – Seego Apr 22 '15 at 19:39
  • Thank you @GuyT. I finally crack the ice.. Thank you for your help. – Seego Apr 23 '15 at 05:26
  • @Seego I'm glad you finally solved your problem. If you are able to awnser your own questions you'll learn faster! – GuyT Apr 23 '15 at 06:28

1 Answers1

0

I think you're looking for the json_decode() function. http://php.net/manual/en/function.json-decode.php

I'm not sure how to address your concerns about the number size without more information about expected inputs.

Fiona C
  • 31
  • 1
  • no actually i can decode the thing manually.. but the problem is with the numeric key contain in this json. – Seego Apr 22 '15 at 17:50
  • right now as i send two number but if i send 100 numbers then how will i decode.. I use following thing $json = file_get_contents($daat); $obj = json_decode($json); $num= $obj->seq_id->{'1'}->validnumber; – Seego Apr 22 '15 at 17:51
  • What specific problem are you running into when you send more numbers? – Fiona C Apr 22 '15 at 18:02