如何从 C# 中的 JSON 字符串中获取一些值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17617594/
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 10:02:55  来源:igfitidea点击:

How to get some values from a JSON string in C#?

c#json

提问by Alon Shmiel

I have a string and I want to get some values from it.

我有一个字符串,我想从中获取一些值。

My strings seem like:

我的字符串看起来像:

string1:

字符串 1:

"{\r\n   \"id\": \"100000280905615\",
 \r\n \"name\": \"Jerard Jones\",
 \r\n   \"first_name\": \"Jerard\",
 \r\n   \"last_name\": \"Jones\",
 \r\n   \"link\": \"https://www.facebook.com/Jerard.Jones\",
 \r\n   \"username\": \"Jerard.Jones\",
 \r\n   \"gender\": \"female\",
 \r\n   \"locale\": \"en_US\"\r\n}"

string2:

字符串2:

"{\r\n   \"id\": \"100000390001929\",
  \r\n   \"name\": \"\u05d1\u05d2\u05e8\u15dc\u25d9 \u05d1\u05e8\u05d5\u05e9\",
  \r\n   \"first_name\": \"\u05d4\u05d2\u05e7\u02dc\u05d9\",
  \r\n   \"last_name\": \"\u05d1\u05e8\u05d5\u05e9\",
  \r\n   \"link\": "https://www.facebook.com/people/\u05d2\u05d1\u05e@\u05dc\u05d9-\u05d1\u05e8\u05d4\u05e9/100000390001929\",
  \r\n   \"gender\": \"female\",
  \r\n   \"locale\": \"he_IL\"\r\n}"

Unfortunately, there is a situation that a string will be by the same concept, but without some parameters:

不幸的是,有一种情况是字符串将具有相同的概念,但没有一些参数:

string3:

字符串 3:

"{\r\n   \"id\": \"100000390001929\",
  \r\n   \"last_name\": \"\u05d1\u05e8\u05d5\u05e9\",
  \r\n   \"gender\": \"female\",
  \r\n   \"locale\": \"he_IL\"\r\n}"

How can I get the values of: id, first_name, last_name, gender, locale?

如何获得以下值:id, first_name, last_name, gender, locale

Any help appreciated!

任何帮助表示赞赏!

采纳答案by Rubens Mariuzzo

Your strings are JSON formatted, so you will need to parse itinto a object. For that you can use JSON.NET.

您的字符串是 JSON 格式的,因此您需要将其解析为一个对象。为此,您可以使用JSON.NET

Here is an example on how to parse a JSON string into a dynamic object:

下面是一个关于如何将 JSON 字符串解析为动态对象的示例:

string source = "{\r\n   \"id\": \"100000280905615\", \r\n \"name\": \"Jerard Jones\",  \r\n   \"first_name\": \"Jerard\", \r\n   \"last_name\": \"Jones\", \r\n   \"link\": \"https://www.facebook.com/Jerard.Jones\", \r\n   \"username\": \"Jerard.Jones\", \r\n   \"gender\": \"female\", \r\n   \"locale\": \"en_US\"\r\n}";
dynamic data = JObject.Parse(source);
Console.WriteLine(data.id);
Console.WriteLine(data.first_name);
Console.WriteLine(data.last_name);
Console.WriteLine(data.gender);
Console.WriteLine(data.locale);

Happy coding!

快乐编码!

回答by Ligabb

Create a class like this:

创建一个这样的类:

public class Data
{
    public string Id {get; set;}
    public string Name {get; set;}
    public string First_Name {get; set;}
    public string Last_Name {get; set;}
    public string Username {get; set;}
    public string Gender {get; set;}
    public string Locale {get; set;}
}

(I'm not 100% sure, but if that doesn't work you'll need use [DataContract]and [DataMember]for DataContractJsonSerializer.)

(我不是 100% 确定,但如果这不起作用,您将需要使用[DataContract]and [DataMember]for DataContractJsonSerializer。)

Then create JSonSerializer:

然后创建JSonSerializer

private static readonly XmlObjectSerializer Serializer = new DataContractJsonSerializer(typeof(Data));

and deserialize object:

并反序列化对象:

// convert string to stream
byte[] byteArray = Encoding.UTF8.GetBytes(contents);
using(var stream = new MemoryStream(byteArray))
{
    (Data)Serializer.ReadObject(stream);
}

回答by Muhammad Awais

Following code is working for me.

以下代码对我有用。

Usings:

用途:

using System.IO;
using System.Net;
using Newtonsoft.Json.Linq;

Code:

代码:

 using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    using (Stream responseStream = response.GetResponseStream())
                    {
                        using (StreamReader responseReader = new StreamReader(responseStream))
                        {
                            string json = responseReader.ReadToEnd();
                            string data = JObject.Parse(json)["id"].ToString();
                        }
                    }
                }

//json = {"kind": "ALL", "id": "1221455", "longUrl": "NewURL"}

回答by DKR

my string

我的字符串

var obj = {"Status":0,"Data":{"guid":"","invitationGuid":"","entityGuid":"387E22AD69-4910-430C-AC16-8044EE4A6B24443545DD"},"Extension":null}

Following code to get guid:

以下代码获取 guid:

var userObj = JObject.Parse(obj);
var userGuid = Convert.ToString(userObj["Data"]["guid"]);