如何从复杂的Json对象中获取值?

时间:2022-09-25 08:54:23

I am trying to get values from Json objects that all are formed like this one: http://services.runescape.com/m=itemdb_rs/api/catalogue/detail.json?item=4798

我试图从Json对象中获取所有这些对象的值:http://services.runescape.com/m= itemdb_rs/api/cataloggue/detail.json? item=4798

I tried several libraries but none of them resulted in the way I wanted. I want to put the values into specific Datamembers.

我尝试了几个图书馆,但没有一个是我想要的。我想把这些值放到特定的数据量中。

This was my last attempt, it runs but it seems like my Datamembers are not getting any values.

这是我的最后一次尝试,它运行,但似乎我的Datamembers没有得到任何值。

namespace JSON_Data
{
    public partial class Form1 : Form
        public Form1()
        {
            InitializeComponent();
            string jsonString = @"{""item"":{""icon"":""http://services.runescape.com/m=itemdb_rs/4996_obj_sprite.gif?id=4798"",""icon_large"":""http://services.runescape.com/m=itemdb_rs/4996_obj_big.gif?id=4798"",""id"":4798,""type"":""Ammo"",""typeIcon"":""http://www.runescape.com/img/categories/Ammo"",""name"":""Adamant brutal"",""description"":""Blunt adamantite arrow...ouch"",""current"":{""trend"":""neutral"",""price"":305},""today"":{""trend"":""neutral"",""price"":0},""members"":""true"",""day30"":{""trend"":""positive"",""change"":""+2.0%""},""day90"":{""trend"":""positive"",""change"":""+8.0%""},""day180"":{""trend"":""positive"",""change"":""+23.0%""}}}";

            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Item));
            MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
            Item obj = (Item)ser.ReadObject(stream);
        }
    }
}

This is how my class "Item" looks

这就是我的类“项目”的外观

namespace JSON_Data
{
    [DataContract]
    public class Item
    {
        [DataMember]
        public string Icon { get; set; }
        [DataMember]
        public string Icon_large { get; set; }
        [DataMember]
        public int Id { get; set; }
        [DataMember]
        public string Name { get; set; }
        [DataMember]
        public string Description { get; set; }
        [DataMember]
        public string Members { get; set; }
    }
}

2 个解决方案

#1


2  

if you can try the Newtonsoft i can provide a way.. its very good and better approach as far as i think

如果你能试试Newtonsoft我可以提供一个方法。在我看来,这是一种非常好的方法

var ob = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(jsonString);
Item a = ((JObject)ob["item"]).ToObject<Item>();

#2


-1  

There are several JSON serializers you can use in C#. Some have better performance, some have better fault tolerance and others have circular reference treatments.

您可以在c#中使用几个JSON序列化器。有的具有较好的性能,有的具有较好的容错能力,有的具有循环参考治疗。

In your case, I see that you simply want an object without passing it (to a WCF) anywhere. You can follow the second answer of this question: Deserialize JSON into C# dynamic object? Example code copied from that answer:

在您的例子中,我看到您只是想要一个对象,而不将它(传递给WCF)到任何地方。您可以遵循这个问题的第二个答案:将JSON反序列化为c#动态对象?从那个答案复制的示例代码:

dynamic stuff = JsonConvert.DeserializeObject("{ 'Name': 'Jon Smith', 'Address': { 'City': 'New York', 'State': 'NY' }, 'Age': 42 }");

string name = stuff.Name;
string address = stuff.Address.City;

A dynamic object in C# allows you to read a property without declaring a class for it.

c#中的一个动态对象允许您读取一个属性,而不必为它声明一个类。

#1


2  

if you can try the Newtonsoft i can provide a way.. its very good and better approach as far as i think

如果你能试试Newtonsoft我可以提供一个方法。在我看来,这是一种非常好的方法

var ob = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(jsonString);
Item a = ((JObject)ob["item"]).ToObject<Item>();

#2


-1  

There are several JSON serializers you can use in C#. Some have better performance, some have better fault tolerance and others have circular reference treatments.

您可以在c#中使用几个JSON序列化器。有的具有较好的性能,有的具有较好的容错能力,有的具有循环参考治疗。

In your case, I see that you simply want an object without passing it (to a WCF) anywhere. You can follow the second answer of this question: Deserialize JSON into C# dynamic object? Example code copied from that answer:

在您的例子中,我看到您只是想要一个对象,而不将它(传递给WCF)到任何地方。您可以遵循这个问题的第二个答案:将JSON反序列化为c#动态对象?从那个答案复制的示例代码:

dynamic stuff = JsonConvert.DeserializeObject("{ 'Name': 'Jon Smith', 'Address': { 'City': 'New York', 'State': 'NY' }, 'Age': 42 }");

string name = stuff.Name;
string address = stuff.Address.City;

A dynamic object in C# allows you to read a property without declaring a class for it.

c#中的一个动态对象允许您读取一个属性,而不必为它声明一个类。