Home > Article > Backend Development > Extract a certain item value in the json array in text (txt)
1. The project has been connected to the third-party platform. The previous technical staff wrote the return results into a txt file, which contains the name of the orderer, mobile phone number, and Jason array (the array contains the results returned by the third-party platform);
Now I need to extract a certain value from json and submit it to Alipay. I don’t know how to operate
I want to get the value PayDeclareAmount":0.0075 in the array. Does anyone have any ideas?
1. The project has been connected to the third-party platform. The previous technical staff wrote the return results into a txt file, which contains the name of the orderer, mobile phone number, and Jason array (the array contains the results returned by the third-party platform);
Now I need to extract a certain value from json and submit it to Alipay. I don’t know how to operate
I want to get the value PayDeclareAmount":0.0075 in the array. Does anyone have any ideas?
There are two methods:
If the previous formats are similar, starting with n lines, directly skip n lines
, the above data, and then, after the row information read later is spliced together, json_decode(xxx, true);
, and then return based on the field. To read a line use fgets
.
Filter with regular matching.
Because I am sure that as long as the value after PayDeclareAmount
matching rule $checktxt="/^PayDeclareAmount&/"
is defined like this, the question is how to get out the subsequent value?
<code>public function getText(){ $file=APPPATH.'upload/aaa.txt'; $content=file($file); foreach ($content as $cont){ echo $cont."<br>"; } }</code>
I just listed the text and got stuck later
Regular really wants to have an indexOf attribute, which returns the matched index value. If you match the '{' symbol and intercept all of it, you will get a string in json format. Then the rest will be easy
Thanks for the invitation.
<code>json_decode("json字符串", true)["PayDeclareAmount"] 即可。</code>
Let’s learn. Everyone’s answers are really good