0

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"}]}
joe
  • 29
  • 2
  • 7
  • With a JSON parser. Don't try to use `split` or regular expressions for this sort of thing. – ajb Nov 23 '14 at 06:46

1 Answers1

0

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

Community
  • 1
  • 1
Tivie
  • 18,864
  • 5
  • 58
  • 77