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

如何用C#从JSON获取指定数据?读取无输出问题求助

问题排查:Newtonsoft.Json读取JSON无数据输出但完整内容正常

问题描述

使用Newtonsoft.Json包读取JSON文件,尝试将指定数据打印到控制台时无数据输出,但打印完整JSON内容正常。

期望输出示例

Stream: Branch Data
Tag: Tag Data

JSON结构

{
  "Data":
  {
    "Branch": "main",
    "BuildDateStamp": "Sat Jul 16 2022",
    "BuildTimeStamp": "08:38:36 PM CST",
    "RequestedP4ChangeNum": "8138336",
    "Tag": "no_tag"
  }
}

原代码示例

public static async Task ShowBuildManifest()
{            
    // Path to JSON File
    string path = "build_manifest.json";

    // This reads the entire JSON file to a string variable (json)
    string json = File.ReadAllText(path);

    // This will deserialize the JSON string into a JObject (Using the Newtonsoft.Json package)
    //var data = JsonConvert.DeserializeObject(json);
    JObject obj = JObject.Parse(json);

    // access specific data in JObject.
    string branch = (string)obj["Data.Branch"];
    string buildDate = (string)obj["BuildDateStamp"];
    string buildTime = (string)obj["BuildTimeStamp"];
    string change = (string)obj["RequestedP4ChangeNum"];
    string tag = (string)obj["Tag"];

    // print data
    Console.WriteLine($"Stream: {branch}");
    Console.WriteLine($"Change: {change}");
    Console.WriteLine($"Build Date: {buildDate}");
    Console.WriteLine($"Build Time: {buildTime}");
    Console.WriteLine($"Tag: {tag}");
}

问题原因

核心错误是JObject属性访问方式不正确:

  • 代码中用obj["Data.Branch"]试图直接获取嵌套属性,但这种写法会查找顶级名称为Data.Branch的属性,而实际该属性嵌套在Data对象内部,因此返回null。
  • BuildDateStamp、RequestedP4ChangeNum等属性同样属于Data的子属性,直接通过顶级obj访问也会返回null。

解决方案

提供两种可行的修改方式:

方式1:逐层访问嵌套JObject

先获取Data子对象,再从中读取对应属性:

public static async Task ShowBuildManifest()
{            
    string path = "build_manifest.json";
    string json = File.ReadAllText(path);
    JObject obj = JObject.Parse(json);

    // 先获取Data子对象
    JObject dataObj = (JObject)obj["Data"];
    
    // 从Data对象中读取属性
    string branch = (string)dataObj["Branch"];
    string buildDate = (string)dataObj["BuildDateStamp"];
    string buildTime = (string)dataObj["BuildTimeStamp"];
    string change = (string)dataObj["RequestedP4ChangeNum"];
    string tag = (string)dataObj["Tag"];

    Console.WriteLine($"Stream: {branch}");
    Console.WriteLine($"Change: {change}");
    Console.WriteLine($"Build Date: {buildDate}");
    Console.WriteLine($"Build Time: {buildTime}");
    Console.WriteLine($"Tag: {tag}");
}

方式2:使用JSON路径表达式(SelectToken)

利用Newtonsoft.Json的SelectToken方法,通过JSON路径直接定位嵌套属性:

public static async Task ShowBuildManifest()
{            
    string path = "build_manifest.json";
    string json = File.ReadAllText(path);
    JObject obj = JObject.Parse(json);

    // 使用JSON路径获取嵌套属性
    string branch = (string)obj.SelectToken("Data.Branch");
    string buildDate = (string)obj.SelectToken("Data.BuildDateStamp");
    string buildTime = (string)obj.SelectToken("Data.BuildTimeStamp");
    string change = (string)obj.SelectToken("Data.RequestedP4ChangeNum");
    string tag = (string)obj.SelectToken("Data.Tag");

    Console.WriteLine($"Stream: {branch}");
    Console.WriteLine($"Change: {change}");
    Console.WriteLine($"Build Date: {buildDate}");
    Console.WriteLine($"Build Time: {buildTime}");
    Console.WriteLine($"Tag: {tag}");
}

修改后运行代码,即可正常输出目标数据。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 08:34:58