简体   繁体   English

从C#中的Object(反序列化json)中检索数据

[英]Retrieving data from an Object(deserialized json) in C#

I have a JSON string that I am trying to parse, using C#. 我有一个尝试使用C#解析的JSON字符串。 I have used JsonConvert to serialize my data into a JSON string. 我已经使用JsonConvert将数据序列化为JSON字符串。

Here is my sample JSON string: 这是我的示例JSON字符串:

{"names": ["John", "Joe", "Jack"], "nationality": "American"}

I am able to deserialize this string into an object using JsonConvert.DeserializeObject(x); 我可以使用JsonConvert.DeserializeObject(x);将字符串反序列化为对象JsonConvert.DeserializeObject(x);

The problem is, I dont know how to read from the object, using C#. 问题是,我不知道如何使用C#读取对象。 Can someone help me out? 有人可以帮我吗?

A better approach would be to define a class with the expected structure, then using JavaScriptSerializer to deserialize it: 更好的方法是定义一个具有预期结构的类,然后使用JavaScriptSerializer反序列化它:

class NameSet
{
    public IList<string> names { get; set; }
    public string nationality { get; set; }
}

var serializer = new JavaScriptSerializer();
var nameset  = serializer.Deserialize<NameSet>(jsonString);

Create a custom class like this: 创建一个这样的自定义类:

public class CustomData
{
    public string[] Names { get; set; }
    public string Nationality { get; set; }

    public CustomData() { }
}

And use JsonConvert to deserialize yo an object of this type: 并使用JsonConvert反序列化此类型的对象:

CustomData data = JsonConvert.DeserializeObject<CustomData>(x);

The following should suffice: 以下内容就足够了:

public class PeopleGroup {

    public string[] names { get; set; }
    public string nationality { get; set }

}

var myObject = JsonConvert.DeserializeObject<PeopleGroup>(x);

Basically, you create a strongly typed object, and deserialise directly into it. 基本上,您将创建一个强类型的对象,然后直接对其进行反序列化。

public class People
{
  [JsonProperty("names")]
  public List<string> Names;

  [JsonProperty("nationality")]
  public string Nationality;
}

Other answers are technically correct, but using JsonPropertyAttribute is a universally accepted convention. 其他答案在技术上是正确的,但是使用JsonPropertyAttribute是普遍接受的约定。 Then use JsonConvert : 然后使用JsonConvert

var people = JsonConvert.DeserializeObject<People>(x);

If you don't want to actually define a class, you can use an anonymous type: 如果您不想实际定义一个类,则可以使用匿名类型:

string json = "{\"names\": [\"John\", \"Joe\", \"Jack\"], \"nationality\": \"American\"}";

// Just defining the structure of the anonymous type
var x = new { names = new string[0], nationality = string.Empty };

x = JsonConvert.DeserializeAnonymousType(json, x);

You should use dataContractJsonSerializer class, it is faster and most important is it is inbuilt class of .Net Framework. 您应该使用dataContractJsonSerializer类,它速度更快,最重要的是它是.Net Framework的内置类。 I will post solution in my next commment, in that How can we use DataContractJsonSerializer class.Now I cant post solution because in my end net is too slow :(, but I will post today. 我将在下一个评论中发布解决方案,即如何使用DataContractJsonSerializer类。现在我无法发布解决方案,因为最终网络太慢了:(,但是我今天将发布。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM