0
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" ?

1
  • You could start by formatting your Json so we can read it. Commented Jan 29, 2017 at 20:40

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

@sunayyildiz Do you have using System.Linq;?
Thanks Anu, RootObject obj = JsonConvert.DeserializeObject<RootObject>(jsonString); var result2 = obj.GetCardTaskListResult.CardTaskList.Select(p => new {Id = p.Id, Amount = p.Amount}).ToArray(); that code is good work :)
1

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().

5 Comments

Hi Jon, I am insert using Newtonsoft.Json.Linq; using Newtonsoft.Json; but not working " .Select()" instead of SelectToken and SelectTokens
Have you included using System.Linq; at the top of your file?
I am insert using System.Linq. I want to use ToList<MyClassList>.. ı am insert my code ; "List<MyClassList> myClassList = new List<MyClassList>(); myClassList = JObject.Parse(responseText)["GetMyClassListResult"]["MyClassList"].Select(item => new { Id = item["Id"], Amount = item["Amount"] }).ToList<MyClassListt>();" but not working...
You could use ToList() instead of ToArray(), but this uses an anonymous type so unless you are only going to use the list locally (or you use dynamic - but don't) you'll need to create your own type as per Anu's answer.
Thanks Jon, RootObject obj = JsonConvert.DeserializeObject<RootObject>(jsonString); var result2 = obj.GetCardTaskListResult.CardTaskList.Select(p => new {Id = p.Id, Amount = p.Amount}).ToArray(); that code is good work :)
1

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. :)

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.