我的JSON字符串具有嵌套值.
就像是
"[{"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"));
}
}
Run Code Online (Sandbox Code Playgroud)
pto*_*mli 12
如果您知道要检索的数据的基本结构,那么正确表示它是有意义的.你会得到类型安全等各种细节;)
public static class TweetThingy {
public int listed_count;
public Status status;
public static class Status {
public int retweet_count;
}
}
List<TweetThingy> tt = mapper.readValue(..., new TypeReference<List<TweetThingy>>() {});
System.out.println(tt.get(0).status.retweet_count);
Run Code Online (Sandbox Code Playgroud)
尝试类似的东西.如果你使用JsonNode,你的生活会更轻松.
JsonNode node = mapper.readValue("[{\"listed_count\":1720,\"status\":{\"retweet_count\":78}}]]", JsonNode.class);
System.out.println(node.findValues("retweet_count").get(0).asInt());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15547 次 |
| 最近记录: |