简体   繁体   中英

How can I get values from a complex Json object?

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

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.

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

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

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

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.

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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