我的JSON字符串具有嵌套值。Java JSON -Jackson-嵌套元素
喜歡的東西
"[{"listed_count":1720,"status":{"retweet_count":78}}]"
我想retweet_count
的價值。
我正在使用傑克遜。
下面的代碼輸出「{retweet_count=78}
」而不是78
。我想知道是否可以通過PHP的方式獲得嵌套值,例如status->retweet_count
。謝謝。
import java.io.IOException;
import java.util.List;
import java.util.Map;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
public class tests {
public static void main(String [] args) throws IOException{
ObjectMapper mapper = new ObjectMapper();
List <Map<String, Object>> fwers = mapper.readValue("[{\"listed_count\":1720,\"status\":{\"retweet_count\":78}}]]", new TypeReference<List <Map<String, Object>>>() {});
System.out.println(fwers.get(0).get("status"));
}
}
這是可以預料的:'status'的va; ue是一個Map不是嗎?你只需要再次用「retweet_count」來調用'get()'。然而,我同意其中一個建議使用'readTree()'而不是'JsonNode'的方法 - 更容易遍歷。 – StaxMan