sunay yildiz
sunay yildiz

Reputation: 79

How to get json data using c#?

string json string = {\"GetMyClassListResult\":{\"MyClassList\":[{\"Id\":1,\"Amount\":\"5,00\"},{\"Id\":2,\"Amount\":\"10,00\"},{\"Id\":3,\"Amount\":\"20,00\"},{\"Id\":4,\"Amount\":\"25,00\"}],\"ReturnValues\":{\"ErrorCode\":1,\"ErrorDescription\":\"Successful\"}}}

How do get "Id":1" and "Amount":"5,00" ?

Upvotes: 0

Views: 67

Answers (4)

Anu Viswan
Anu Viswan

Reputation: 18153

First, you would need to declare a class as following.

class MyClass
{
    [JsonProperty(PropertyName = "Id")]
    public int Id { get; set; }

    [JsonProperty(PropertyName = "Amount")]
    public string Amount { get; set; } 
}

and then, you can do the following in your method

var Jsonobj = JObject.Parse(json);
var list = JsonConvert.DeserializeObject<MyClass[]>(Jsonobj["GetMyClassListResult"]["MyClassList"].ToString()).ToList<MyClass>();

Hope that helps.

Upvotes: 1

Cameron
Cameron

Reputation: 2594

So you have straight json and you're trying to convert it into an object to get some meaningful values?

I personally prefer working with classes where I can. I will throw my raw json into a json to C# converter. I like JsonUtils personally. (I have no affiliation, just a great free service.) There are some others out there, but it seems to be the most robust.

If you throw your raw json into there, you get the following classes:

public class MyClassList
{
    public int Id { get; set; }
    public string Amount { get; set; }
}

public class ReturnValues
{
    public int ErrorCode { get; set; }
    public string ErrorDescription { get; set; }
}

public class GetMyClassListResult
{
    public IList<MyClassList> MyClassList { get; set; }
    public ReturnValues ReturnValues { get; set; }
}

public class Example
{
    public GetMyClassListResult GetMyClassListResult { get; set; }
}

Alright. Now that we have our models, we can deserialize the json. The most popular library for manipulating json is Newtonsoft.Json, which is available on NuGet or direct download.

You'll need to add a reference to that dll if you choose to download it; Nuget will auto-reference it when you install it.

At the top of your class file, you'll need to add

using Newtonsoft.Json;

along with your other using statements.

In your method you'll call JsonConvert.Deserialize<List<Example>>(json), which will give you your collection in POCOs.

Since there is only one object in the collection you can call .First() on the collection and then access the two values via the Id and Amount properties. You will need to make sure that System.Linq is in your using statements as well.

Full code:

var json = @"{\"GetMyClassListResult\":{\"MyClassList\":[{\"Id\":1,\"Amount\":\"5,00\"},{\"Id\":2,\"Amount\":\"10,00\"},{\"Id\":3,\"Amount\":\"20,00\"},{\"Id\":4,\"Amount\":\"25,00\"}],\"ReturnValues\":{\"ErrorCode\":1,\"ErrorDescription\":\"Successful\"}}}";

var collection = JsonConvert.Deserialize<List<Example>>(json);

var x = collection.First();
var amount = x.Amount;
var id = x.Amount;

You're then free to manipulate or work with those variables as you see fit. :)

Upvotes: 1

Jon Davies
Jon Davies

Reputation: 487

Combining the excellent Json.NET package with LINQ you could use:

var result = JObject.Parse(json)["GetMyClassListResult"]["MyClassList"]
                    .Select(item => new { Id = item["Id"], Amount = item["Amount"] })
                    .First();

result has the properties Id and Amount corresponding to the first item in the JSON array with values 1 and "5,00".

If instead you wanted an array of all items, just replace First() with ToArray().

Upvotes: 1

Abhirup Ghosh
Abhirup Ghosh

Reputation: 1

first of all you need a class where you will de-serialize this string to your class object. you need a class which will contain Id and Amount variable.

after that you can de-serialize this string to the object then you can access any data you want.

Upvotes: 0

Related Questions