Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#用JSON复制此代码-JSON和对象_C#_Json_Object - Fatal编程技术网

C#用JSON复制此代码-JSON和对象

C#用JSON复制此代码-JSON和对象,c#,json,object,C#,Json,Object,我对C#比较陌生,我希望复制以下JSON表达式: {"itemData":[{"pile":"club","id":"100997087277"}]} 目前我有以下几种方法: public class MoveItemValues { public string pile; public Int64 id; } public class MoveItemRequestValues { public MoveItemValues itemData; } interna

我对C#比较陌生,我希望复制以下JSON表达式:

{"itemData":[{"pile":"club","id":"100997087277"}]}
目前我有以下几种方法:

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);
}
这将产生:

"{"itemData":{"pile":"trade","id":100997087277}}"
但正如你所看到的,方括号不见了

我将如何完成这一目标?

使用列表

公共类moveItemRequestValue
{
公共清单项目数据;
}

itemData是json字符串中的一个数组

仅供参考:您需要遵循C#命名指南。属性和方法应为Pascal大小写

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();
}
公共类moveItemValue
{
公共串桩;
公共Int64 Id;
}
公共类MoveItemRequestValue
{
公共IList项目数据;
公共MoveItemRequestValues()
{
ItemData=新列表();
}
}
静态void MoveItem(Int64 itemId,字符串堆)
{
字符串moveItemResponse;
MoveItemRequestValues bodyContent=新的MoveItemRequestValues();
bodyContent.ItemData=新列表()
{
新MoveItemValues{Pile=Pile,Id=itemId}
};
var camelCaseFormatter=新的JsonSerializerSettings
{
ContractResolver=新的CamelCasePropertyNamesContractResolver()
};
字符串jsonContent=JsonConvert.SerializeObject(bodyContent,camelCaseFormatter);
byte[]byteArray=Encoding.UTF8.GetBytes(jsonContent);
Console.WriteLine(jsonContent);
}
静态void Main(字符串[]参数)
{
移动项目(100997087277,“贸易”);
Console.ReadLine();
}

我认为itemData需要是一个MoveItemValue数组。@Jakobalsen或一个
列表
。仅供参考,C#中公认的风格是使用
long
等别名,而不是
Int64
。您还应该显示
moveItem
方法需要如何更改。这不会同时更改Json字符串中的大小写吗?我更新了答案它使用CamelCasePropertyNamesContractResolvery很好。我唯一想更改的是
Int64
long
@juharr谢谢你指出了外壳。我还同意将Int64改为long.Thank,并感谢您提供有关命名准则的建议-ColdFusion中的camel case始终是习惯!另外,将Int64更新为long。使用其中一个而不是另一个有什么特别的原因吗?
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();
}