随机一言

获取随机一言,简单高效的API接口服务

API接口稳定可用 · 支持高并发请求
总调用次数: 加载中...

API接口信息

接口地址

https://www.yutangxiaowu.cn:3007/api/random/json

请求方法

GET

请求参数

参数名 类型 是否必填 说明
filename string 可选 随机一言的json文件名(a-i.json)

响应格式

JSON

响应参数

参数名 类型 说明
selectedFile string 选择的json文件名
data.hitokoto string 内容
data.type string 类型
data.from string 来源
data.from_who string 创作者
data.length number 长度

代码示例

JavaScript (Axios)
const apiUrl = 'https://yutangxiaowu.cn:3007/api/random/json';
axios.get(apiUrl)
  .then(response => {
    console.log(response.data);
  })
  .catch(error => {
    console.error('请求失败:', error);
  });
JavaScript (Fetch)

const apiUrl = 'https://yutangxiaowu.cn:3007/api/random/json'
fetch(`${apiUrl}`)
    .then(response => response.json())
    .then(data => console.log(data))
    .catch(error => console.error('请求失败:', error));
Python
import requests

api_url = 'https://yutangxiaowu.cn:3007/api/random/json'
try:
    response = requests.get(api_url)
    response.raise_for_status() 
    data = response.json()
    print(data)
except requests.exceptions.RequestException as e:
    print('请求失败:', e)
PHP
$apiUrl = 'https://yutangxiaowu.cn:3007/api/random/json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 

$response = curl_exec($ch);
if ($response === false) {
    echo '请求失败:' . curl_error($ch);
} else {
    $data = json_decode($response, true);
    print_r($data);
}
curl_close($ch);
Java
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class HttpDemo {
    public static void main(String[] args) {
        String apiUrl = "https://yutangxiaowu.cn:3007/api/random/json";
        try {
            URL url = new URL(apiUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setRequestProperty("Accept", "application/json");

            int responseCode = connection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String inputLine;
                StringBuilder response = new StringBuilder();
                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
                in.close();
                System.out.println(response.toString());
            } else {
                System.out.println("请求失败,响应码:" + responseCode);
            }
            connection.disconnect();
        } catch (IOException e) {
            System.out.println("请求失败:" + e.getMessage());
        }
    }
}
C#
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;

class Program
{
    static async Task Main(string[] args)
    {
        string apiUrl = "https://yutangxiaowu.cn:3007/api/random/json";
        using (HttpClient client = new HttpClient())
        {
            try
            {
                HttpResponseMessage response = await client.GetAsync(apiUrl);
                response.EnsureSuccessStatusCode(); 
                string responseBody = await response.Content.ReadAsStringAsync();
                JObject data = JObject.Parse(responseBody);
                Console.WriteLine(data.ToString());
            }
            catch (HttpRequestException e)
            {
                Console.WriteLine("请求失败:" + e.Message);
            }
        }
    }
}
Ruby
require 'net/http'
require 'json'

api_url = 'https://yutangxiaowu.cn:3007/api/random/json'
uri = URI(api_url)

begin
  response = Net::HTTP.get(uri)
  data = JSON.parse(response)
  puts data
rescue StandardError => e
  puts "请求失败:#{e.message}"
end
Go
package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
)

func main() {
	apiUrl := "https://yutangxiaowu.cn:3007/api/random/json"
	resp, err := http.Get(apiUrl)
	if err != nil {
		fmt.Println("请求失败:", err)
		return
	}
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Println("读取响应体失败:", err)
		return
	}

	var data interface{}
	err = json.Unmarshal(body, &data)
	if err != nil {
		fmt.Println("解析JSON失败:", err)
		return
	}

	fmt.Println(data)
}
Swift
import Foundation

let apiUrl = URL(string: "https://yutangxiaowu.cn:3007/api/random/json")!
var request = URLRequest(url: apiUrl)
request.httpMethod = "GET"

let task = URLSession.shared.dataTask(with: request) { data, response, error in
    if let error = error {
        print("请求失败:", error.localizedDescription)
        return
    }
    guard let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode == 200 else {
        print("响应状态码异常")
        return
    }
    if let data = data {
        do {
            let json = try JSONSerialization.jsonObject(with: data, options: [])
            print(json)
        } catch {
            print("解析JSON失败:", error.localizedDescription)
        }
    }
}

task.resume()
Kotlin
import java.net.HttpURLConnection
import java.net.URL
import java.io.BufferedReader
import java.io.InputStreamReader

fun main() {
    val apiUrl = "https://yutangxiaowu.cn:3007/api/random/json"
    try {
        val url = URL(apiUrl)
        val connection = url.openConnection() as HttpURLConnection
        connection.requestMethod = "GET"
        connection.setRequestProperty("Accept", "application/json")

        val responseCode = connection.responseCode
        if (responseCode == HttpURLConnection.HTTP_OK) {
            val inStream = BufferedReader(InputStreamReader(connection.inputStream))
            var inputLine: String?
            val response = StringBuilder()
            while (inStream.readLine().also { inputLine = it } != null) {
                response.append(inputLine)
            }
            inStream.close()
            println(response.toString())
        } else {
            println("请求失败,响应码:$responseCode")
        }
        connection.disconnect()
    } catch (e: Exception) {
        println("请求失败:${e.message}")
    }
}

在线测试工具