1

I'm relatively new to C# and I'm looking to replicate this following JSON expression:

{"itemData":[{"pile":"club","id":"100997087277"}]}

At present I have the following methods:

public class MoveItemValues
{
    public string pile;
    public Int64 id;
}

public class MoveItemRequestValues
{
    public MoveItemValues itemData;
}

internal string moveItem(Int64 itemId, string pile)
{
    string moveItemResponse;
    MoveItemRequestValues bodyContent = new MoveItemRequestValues();
    bodyContent.itemData = new MoveItemValues();
    bodyContent.itemData.pile = pile;
    bodyContent.itemData.id = itemId;
    string jsonContent = JsonConvert.SerializeObject(bodyContent);
    byte[] byteArray = Encoding.UTF8.GetBytes(jsonContent);
    Console.WriteLine(jsonContent);
}

This produces:

"{"itemData":{"pile":"trade","id":100997087277}}"

But as you can see the square brackets are missing.

How would I go about finishing off achieving this?

3
  • I think itemData needs to be an array of MoveItemValues. Commented Nov 10, 2015 at 14:52
  • @JakobOlsen Or a List<MoveItemValues>. Commented Nov 10, 2015 at 14:53
  • FYI the accepted style in C# is to use aliases like long instead of Int64. Commented Nov 10, 2015 at 14:55

2 Answers 2

1

itemData is an array in the json string.

FYI: You need to follow the C# naming guidelines. Property and method should be Pascal case.

public class MoveItemValues
{
    public string Pile;
    public Int64 Id;
}

public class MoveItemRequestValues
{
    public IList<MoveItemValues> ItemData;

    public MoveItemRequestValues()
    {
        ItemData = new List<MoveItemValues>();
    }
}

static void MoveItem(Int64 itemId, string pile)
{
    string moveItemResponse;
    MoveItemRequestValues bodyContent = new MoveItemRequestValues();
    bodyContent.ItemData = new List<MoveItemValues>()
    {
        new MoveItemValues {Pile = pile, Id = itemId}
    };
    var camelCaseFormatter = new JsonSerializerSettings
    {
        ContractResolver = new CamelCasePropertyNamesContractResolver()
    };
    string jsonContent = JsonConvert.SerializeObject(bodyContent, camelCaseFormatter);
    byte[] byteArray = Encoding.UTF8.GetBytes(jsonContent);
    Console.WriteLine(jsonContent);
}

static void Main(string[] args)
{
    MoveItem(100997087277, "trade");
    Console.ReadLine();
}

enter image description here

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

5 Comments

Won't that change the casing in the Json string as well?
I updated the answer which uses CamelCasePropertyNamesContractResolver
Very nice the only thing I would change is Int64 to long.
@juharr Thank you for pointing out the casing. I also agree on changing Int64 to long.
Thanks, and thanks for the advice on naming guidelines - always been habit to camel case in ColdFusion! Also, updated Int64 to long. Is there any particular reason why to use one rather than the other?
0

Use List<>

public class MoveItemRequestValues
{
    public List<MoveItemValues> itemData;
}

1 Comment

You should also show how the moveItem method needs to change.

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.