How to split the JSON of master and slave structures

Source: Internet
Author: User
$a = ' {"Head": {"id": "4", "Userno": "12345", "username": "CDZ1"}, "Body": [{"OrderNo": "1", "Price": "15.00", "qty": "1"},{" OrderNo ":" 1 "," Price ":" 9.00 "," Qty ":" 1 "}]} ';

For such a jsoin, I have to get the field value of the head part and the field value of the body part separately, such as the Userno of the head part, and the price of the body part.


Reply to discussion (solution)

You Print_r (Json_decode ($a, 1)); Look, you know how to get the weight.

You Print_r (Json_decode ($a, 1)); Look, you know how to get the weight.




return this,
Array ([head] = = Array ([id] = 4 [Userno] = 13538750770 [Username] = CDZ1) [body] = = Array ([0] =&gt ; Array ([OrderNo] = 1 [Price] = 15.00 [Qty] + 1) [1] = = Array ([OrderNo] = 1 [Foodno] = 3 [Price] = 9.00 [Qty] + 1)) Array ([0] = orange [1] = banana [2] + apple [3] = raspberry)

Circus Master Help.

The data you want is not taken out of such an array, your teacher should have spanked.

Array ('  head ' = =   Array (    ' id ' = ' 4 ',    ' userno ' = ' 12345 ', ' username ' = ' CDZ1 '    ,  ) ,  ' body ' = =   Array (    0 = =     Array (      ' orderno ' = ' 1 ',      ' price ' = ' 15.00 ',      ' Qty ' + ' 1 ',    ),    1 = =     Array (      ' orderno ' = ' 1 ',      ' price ' = ' 9.00 ',      ' qty ' = = ' 1 ',    ),)  ,)

 
  Head->userno;echo '
Price: '. $obj->body[0]->price;/////or: Echo '


"; $ary =json_decode ($a, true); Echo ' Userno: '. $ary [' head '] [' Userno '];echo '
Price: '. $ary [' body '][0][' price '];? >

Using Json_decode (), you can convert the JSON data format and then call it in the form of an array.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.