You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#如何通过HTTP GET请求获取远程JSON数据并解析CurrentVersion值

不同开发环境下的实现方案

核心逻辑统一为三步:发送HTTP GET请求拉取JSON内容、解析JSON结构、提取CurrentVersion字段赋值为字符串变量。

Python 实现

依赖requests库,缺依赖可执行pip install requests安装:

import requests

# 发送GET请求
resp = requests.get("http://example.com/data.json")
# 校验请求状态,出错会直接抛出异常
resp.raise_for_status()
# 解析JSON并提取字段
current_version: str = resp.json()["CurrentVersion"]

Node.js 实现

原生HTTP模块实现,无需额外依赖:

const http = require('http')

http.get('http://example.com/data.json', res => {
  let rawData = ''
  res.on('data', chunk => rawData += chunk)
  res.on('end', () => {
    const jsonData = JSON.parse(rawData)
    const currentVersion = jsonData.CurrentVersion
  })
})

浏览器端 JavaScript 实现

注意需要目标域名配置允许跨域访问,否则会触发跨域拦截:

fetch('http://example.com/data.json')
  .then(res => res.json())
  .then(jsonData => {
    const currentVersion = jsonData.CurrentVersion
  })

Shell 实现

依赖curl和jq工具,缺依赖可安装对应系统的软件包:

current_version=$(curl -s http://example.com/data.json | jq -r '.CurrentVersion')

Java 11+ 实现

依赖Jackson JSON解析库:

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public class GetVersion {
    public static void main(String[] args) throws Exception {
        HttpClient client = HttpClient.newHttpClient();
        HttpRequest req = HttpRequest.newBuilder().uri(URI.create("http://example.com/data.json")).build();
        String respBody = client.send(req, HttpResponse.BodyHandlers.ofString()).body();
        JsonNode jsonNode = new ObjectMapper().readTree(respBody);
        String currentVersion = jsonNode.get("CurrentVersion").asText();
    }
}

内容的提问来源于stack exchange,提问作者RvS01

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 22:06:01