在C#中使用多个数组读取Json文件[已关闭]

nue99wik  于 2023-01-27  发布在  C#
关注(0)|答案(1)|浏览(163)

**已关闭。**此问题需要debugging details。当前不接受答案。

编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将有助于其他人回答问题。
9小时前关门了。
这篇文章是9小时前编辑并提交审查的。
Improve this question
我想从this这样的文件的“Result”部分读取数据。我该怎么做?我已经尝试过Newtonsoft.Json
我试试这个

OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "JSON (*.json) | *.json";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
    using (StreamReader r = new StreamReader(openFileDialog.FileName))
    {
        JsonSerializer serializer = new JsonSerializer();
        DataSet[] dataSet = JsonConvert.DeserializeObject<DataSet[]>(r.ReadToEnd().Remove(2,16));
        DataTable dataTable = dataSet[4].Tables["Result"];
        Console.Write(dataTable.Rows.Count);
    }
}

但我有一个例外
更新
这是一个JSON字符串

{
    "Version": 5,
    "Cars": [
        {
            "BallastKG": 0,
            "CarId": 0,
            "Driver": {
                "Guid": "76561198257336393",
                "GuidsList": [
                    "76561198257336393"
                ],
                "Name": "Luca Sciortino",
                "Nation": "ITA",
                "Team": ""
            }
        }
    ],
    "Events": [
        {
            "CarId": 5,
            "Driver": {
                "Guid": "76561198257336393",
                "GuidsList": [
                    "76561198257336393",
                    "76561198864784779",
                    "76561199001910433",
                    "76561199041353685",
                    "76561198942422138"
                ],
                "Name": "Luca Sciortino",
                "Nation": "ITA",
                "Team": ""
            },
            "ImpactSpeed": 2.3896868,
            "OtherCarId": 0,
            "OtherDriver": {
                "Guid": "",
                "GuidsList": null,
                "Name": "",
                "Nation": "",
                "Team": ""
            },
            "RelPosition": {
                "X": -0.83225566,
                "Y": -0.33289996,
                "Z": 1.5129334
            },
            "Type": "COLLISION_WITH_ENV",
            "WorldPosition": {
                "X": -108.04043,
                "Y": 394.7313,
                "Z": -343.52805
            },
            "Timestamp": 1674760223,
            "AfterSessionEnd": false
        }
    ],
    "Laps": [
        {
            "BallastKG": 0,
            "CarId": 5,
            "CarModel": "msc_radical_pr6",
            "Cuts": 0,
            "DriverGuid": "76561198257336393",
            "DriverName": "Luca Sciortino",
            "LapTime": 187994,
            "Restrictor": 0,
            "Sectors": [
                187994
            ],
            "Timestamp": 1674760359,
            "Tyre": "PZERO S",
            "ContributedToFastestLap": false
        }
    ],
    "Result": [
        {
            "BallastKG": 0,
            "BestLap": 184985,
            "CarId": 9,
            "CarModel": "msc_radical_pr6",
            "DriverGuid": "76561198864784779",
            "DriverName": "Leo Cucca",
            "Restrictor": 0,
            "TotalTime": 2485942,
            "NumLaps": 5,
            "ClassID": "00000000-0000-0000-0000-000000000000",
            "GridPosition": 6,
            "HasPenalty": false,
            "PenaltyTime": 0,
            "LapPenalty": 0,
            "Disqualified": false
        }
    ],
    "Penalties": null,
    "TrackConfig": "",
    "TrackName": "msc_monte_erice_2020",
    "Type": "PRACTICE",
    "Date": "2023-01-26T21:00:39.319133445+01:00",
    "SessionFile": "2023_1_26_21_0_PRACTICE.json",
    "SessionConfig": {
        "session_type": 1,
        "name": "Practice",
        "time": 60,
        "laps": 0,
        "is_open": 1,
        "wait_time": 0,
        "visibility_mode": 0,
        "qualifying_type": 0,
        "qualifying_number_of_laps_to_average": 0,
        "count_out_lap": true,
        "disable_push_to_pass": false
    },
    "ChampionshipID": "",
    "RaceWeekendID": ""
}
wfsdck30

wfsdck301#

您需要清除问题。如果需要将结果节提取为DataTable

var json =  r.ReadToEnd();
DataTable dataTable= JObject.Parse(json)["Result"].ToObject<DataTable>();

// how to use
string carModel= (string) dataTable.Rows[0]["CarModel"]; //msc_radical_pr6

如果需要数据集

DataSet dataSet = new DataSet("Tables");
dataSet.Tables.Add(dataTable);
dataSet.Tables[0].TableName="dataTable";

但它更容易解析

var json =  r.ReadToEnd();
var result = JObject.Parse(json)["Result"][0];
string carModel= (string) result["CarModel"]; //msc_radical_pr6

相关问题