如何在c# [duplicate]中访问JSON对象

时间:2021-12-17 21:33:07

This question already has an answer here:

这个问题已经有了答案:

I receive the following Json through a web service:

我通过web服务收到以下Json:

  {
     report: {
      Id: "aaakkj98898983"
     }
  }

I want to get value of the Id. How to do this in C#? THANKS

我想要得到Id的值,在c#中怎么做?谢谢

1 个解决方案

#1


32  

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();
        }
    }
}   

#1


32  

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();
        }
    }
}