1

I am using Newtonsoft.dll and this is the scenario,

List<int> listNumbers = new List<int>() { 1, 2, 3, 4, 5};
        var result = JsonConvert.SerializeObject(listNumbers);

the result is ,

[1,2,3,4,5]

But I want the following type of result,

 [{"cardvalue":1},{"cardvalue":2},{"cardvalue":3},{"cardvalue":4},{"cardvalue":5}]

How can I do this ?

1 Answer 1

2

Try-

    var result = listNumbers.Select(a => new
                {
                    cardvalue = a
                });
var jsonResult = JsonConvert.SerializeObject(result);

or serialize result directly -

  var result = JsonConvert.SerializeObject(listNumbers.Select(a => new
        {
            cardvalue = a
        }));
Sign up to request clarification or add additional context in comments.

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.