如何在 C# 中访问 JSON 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16459155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:03:07 来源:igfitidea点击:
how to access JSON object in C#
提问by Bathiya Ladduwahetty
I receive the following Json through a web service:
我通过网络服务收到以下 Json:
{
report: {
Id: "aaakkj98898983"
}
}
I want to get value of the Id. How to do this in C#? THANKS
我想获得 Id 的值。如何在 C# 中做到这一点?谢谢
采纳答案by Maloric
First, download Newtonsoft's Json Library, then parse the json using JObject. This allows you to access the properties within pretty easily, like so:
首先,下载Newtonsoft 的 Json 库,然后使用 JObject 解析 json。这使您可以非常轻松地访问其中的属性,如下所示:
using System;
using Newtonsoft.Json.Linq;
namespace testClient
{
class Program
{
static void Main()
{
var myJsonString = "{report: {Id: \"aaakkj98898983\"}}";
var jo = JObject.Parse(myJsonString);
var id = jo["report"]["Id"].ToString();
Console.WriteLine(id);
Console.Read();
}
}
}

