我该如何分割这个字符串?

时间:2014-11-23 06:35:32

标签: java javafx javafx-8

如何切割这根巨大的绳子?我需要first ID,"的数据。最好是字符串输出为:

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

这可能吗?或者,如果没有任何其他方式获取名称:" site1"和has_doneit:true会很好。

   {"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"}]}

1 个答案:

答案 0 :(得分:0)

这似乎是一个JSON字符串,所以使用Json Parser ......

PHP:JSON_decode

$parsedStr = json_decode($yourString, true);

您可以访问$parsedStr['sites']

中的网站数组

因此,要访问第一个网站的ID:

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

爪哇

在SO Decoding JSON String in Java

中查看此答案