Lewis Hyett
Lewis Hyett

Reputation: 1

Iterating through JSON Result

I would like help Iterating through the JSON response in C#. I want to output the Item.Name. Do I need to add a property for every token in the json body within my class?

What I have below isn't working. Appreciate the help.

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text.Json;
using System.Threading.Tasks;

namespace RuneScape_GE_API
{
    class Program
    {
        static void Main(string[] args)
        {
            var url = "https://secure.runescape.com/m=itemdb_rs/api/catalogue/detail.json?item=21787";

            HttpClient client = new HttpClient();

            client.BaseAddress = new Uri(url);
            client.DefaultRequestHeaders.Accept.Add(
         new MediaTypeWithQualityHeaderValue("application/json"));
            HttpResponseMessage response = client.GetAsync(url).Result; 
            if (response.IsSuccessStatusCode)
            {
                // Parse the response body.
                var jsonResult = response.Content.ReadAsStringAsync().Result;
                item citem = JsonConvert.DeserializeObject<item>(jsonResult);
                
            }
        }
    }

    public class item
    {
        public string name { get; set; }
    }
}

Example JSON: { "item":{ "icon":"https://secure.runescape.com/m=itemdb_rs/1624875191508_obj_sprite.gif?id=21787", "icon_large":"https://secure.runescape.com/m=itemdb_rs/1624875191508_obj_big.gif?id=21787", "id":21787, "type":"Miscellaneous", "typeIcon":"https://www.runescape.com/img/categories/Miscellaneous", "name":"Steadfast boots", "description":"A pair of powerful-looking boots.", "current":{ "trend":"neutral", "price":"4.8m" }, "today":{ "trend":"neutral", "price":0 }, "members":"true", "day30":{ "trend":"negative", "change":"-6.0%" }, "day90":{ "trend":"negative", "change":"-10.0%" }, "day180":{ "trend":"negative", "change":"-19.0%" } } }

Upvotes: 0

Views: 1058

Answers (3)

Lewis Hyett
Lewis Hyett

Reputation: 1

Answer: namespace RuneScape_GE_API { class Program { static void Main(string[] args) { var url = "https://secure.runescape.com/m=itemdb_rs/api/catalogue/detail.json?item=21787";

        HttpClient client = new HttpClient();

        client.BaseAddress = new Uri(url);
        client.DefaultRequestHeaders.Accept.Add(
     new MediaTypeWithQualityHeaderValue("application/json"));
        HttpResponseMessage response = client.GetAsync(url).Result;
        if (response.IsSuccessStatusCode)
        {
            // Parse the response body.
            var jsonResult = response.Content.ReadAsStringAsync().Result;
            var dictionary = JsonConvert.DeserializeObject<Dictionary<string, RuneScapeItem>>(jsonResult);
            foreach (var item in dictionary)
            {
                var key = item.Key;
                var RsItem = item.Value;

                Console.WriteLine(RsItem.PersonId);
                Console.ReadKey();

            }
        }
    }

    public class RuneScapeItem
    {
        [JsonProperty("name")]
        public string PersonId { get; set; }
    }
}

}

Upvotes: 0

Tran Phong
Tran Phong

Reputation: 113

Simply, instead of using JsonConvert which you have to define an object for it to convert, you can use the class JObject in the namespace Newtonsoft.Json.Linq

   var jsonResult = response.Content.ReadAsStringAsync().Result;
   JObject result = JObject.Parse(jsonResult);
   Console.WriteLine(result["item"]);

Further more, you can access your json result with this simple statement of code:

result["item"]["current"]["trend"]

For more information, click here for the Newtonsoft docs.

Upvotes: 2

Leonardo Lima
Leonardo Lima

Reputation: 646

Your class item should have a property class called Item that contains a property string

e.g:

public class YourClass
{
    public Item item { get; set; }

    public class Item
    {
        public string name { get; set; }
    }
}

Upvotes: 0

Related Questions