简体   繁体   中英

How would I split this string?

How would I go about cutting this giant string? I need the data of the first ID ," preferably for the string output to be:

"id":1,"name":"site1","has_doneit":true,"destination":"4613"

Is this possible? Or if not any other ways of grabbing the name:"site1" and the has_doneit:true would be perfectly fine.

   {"needs_complete":true,"has_done":true,"sites":[
    {"id":1,"name":"site1","has_doneit":true,"destination":"4613"},{"id":2,"name":"site2","has_doneit":true,"destination":"4613"},{"id":3,"name":"site3","has_doneit":true,"destination":"4339"},{"id":4,"name":"site4","has_doneit":true,"destination":"4340"},{"id":5,"name":"site5","has_doneit":true,"destination":"4341"},
    {"id":6,"name":"site6","has_doneit":true,"destination":"4622"},{"id":7,"name":"site7","has_doneit":true,"destination":"4623"},{"id":8,"name":"site8","has_doneit":true,"destination":"4828"},
    {"id":9,"name":"site9","has_doneit":true,"destination":"4829"},{"id":10,"name":"site10","has_doneit":true,"destination":"4861"}]}

That seems like a JSON string so use a Json Parser...

PHP: JSON_decode

$parsedStr = json_decode($yourString, true);

you can access sites array in $parsedStr['sites']

So, to access the id of the first site:

echo $parsedStr['sites'][0]['id'];

Java

check this answer in SO Decoding JSON String in Java

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM