如何仅从每行中提取链接?
这样输入:
"url": "https://i.ytimg.com/vi/XGM6sHIJuho/hqdefault1.jpg",
"og:image": "https://i.ytimg.com/vi/XGM6sHIJuho/hqdefault2.jpg",
"twitter:image":"https://i.ytimg.com/vi/XGM6sHIJuho/hqdefault3.jpg",
看起来像这样:
https://i.ytimg.com/vi/XGM6sHIJuho/hqdefault1.jpg
https://i.ytimg.com/vi/XGM6sHIJuho/hqdefault2.jpg
https://i.ytimg.com/vi/XGM6sHIJuho/hqdefault3.jpg
感谢您的时间。
答案 0 :(得分:0)
如果您使用谷歌的Gson库,那就非常直接了。
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import java.util.List;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
// write your code here
String json = "{\"url\": \"https://i.ytimg.com/vi/XGM6sHIJuho/hqdefault1.jpg\"," +
" \"og:image\": \"https://i.ytimg.com/vi/XGM6sHIJuho/hqdefault2.jpg\"," +
" \"twitter:image\":\"https://i.ytimg.com/vi/XGM6sHIJuho/hqdefault3.jpg\"}";
Gson gson = new Gson();
JsonObject obj = gson.fromJson(json, JsonObject.class);
List<String> links = obj.entrySet().stream()
.map(entry -> entry.getValue().getAsString())
.collect(Collectors.toList());
links.forEach(System.out::println);
}
}
请记住添加花括号以使其成为有效的json对象。