2

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; }
    }
}
  • 3
    Just paste your source json in a converter like [this one](http://json2csharp.com/) (First result on Google). Your properties name are not the same as those in the Json, that's why it's not working. – Pierre-Luc Pineault Nov 08 '15 at 18:25
  • @Pierre-LucPineault, Many thanks for answering so quickly! I'm on it now. – S. Van den Wyngaert Nov 08 '15 at 18:36
  • i noticed the string has some extra properties value which your item class does not have. so simply Deserilization can not return the object. ur input seems wrong to me – Deepak Sharma Nov 08 '15 at 19:30

2 Answers2

2

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>();
Deepak Sharma
  • 4,124
  • 1
  • 14
  • 31
  • thanks alot! This is what I had in mind, It works really well with all Json files i wanted to. I tried working with Newtonsoft during this project but I jist did one little thing different that made it not work. – S. Van den Wyngaert Nov 08 '15 at 20:05
  • no problem. I'm glad that resolved the issue. Go Ahead. and enjoy code/knowledge sharing. Happy Coding. – Deepak Sharma Nov 08 '15 at 20:07
-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.

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.

Community
  • 1
  • 1
Believe2014
  • 3,894
  • 2
  • 26
  • 46