Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.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# 使用.net core c将json字符串从一种类型转换为另一种类型#_C#_Json - Fatal编程技术网

C# 使用.net core c将json字符串从一种类型转换为另一种类型#

C# 使用.net core c将json字符串从一种类型转换为另一种类型#,c#,json,C#,Json,我正在努力将下面的输入json转换为输出json,因为这是需要调用的格式 hubspot api提交表单。我在Azure函数中使用.NETCore编写本文 输入Json { "Email":"myemail@test.com", "Phone":"12345678", "Address":"address 1" } 输出json { "f

我正在努力将下面的输入json转换为输出json,因为这是需要调用的格式 hubspot api提交表单。我在Azure函数中使用.NETCore编写本文

输入Json

{
    "Email":"myemail@test.com",
    "Phone":"12345678",
    "Address":"address 1"
}
输出json

{
  "fields": [
    {
      "name": "Email",
      "value": "myemail@test.com"
    },
    {
      "name": "Phone",
      "value": "12345678"
    },
    {
      "name": "Address",
      "value": "address 1"
    }
  ]
}
我使用

IDictionary<string, string> dictionary = JsonConvert.DeserializeObject<IDictionary<string, string>>(inputJson);
IDictionary dictionary=JsonConvert.DeserializeObject(inputJson);
但这给了我键值对,而不是名值对

我希望输出如上所述

非常感谢您提供的任何帮助/示例代码。

如果您不希望“Key”作为字段名,您可以创建自己的“NameValuePair”类/结构:

public class FieldContainer
{
    [JsonProperty("fields")]
    public IEnumerable<NameValuePair> Fields { get; set; }
}

public struct NameValuePair
{
    [JsonProperty("name")]
    public string Name { get; set; }
    [JsonProperty("value")]
    public string Value { get; set; }
    
    public NameValuePair(string name, string value)
    {
        Name = name;
        Value = value;
    }
}

请参阅以获取演示。

最简单的方法是获取json并将其转换为字典。循环每个KeyValuePair,并使用LINQ创建字段列表。获得字段列表后,创建RootObject

public class RootObject
{
    [JsonProperty("fields")]
    public List<Field> Fields { get; set; }
}
public class Field
{
    [JsonProperty("name")]
    public string Name { get; set; }
    [JsonProperty("value")]
    public string Value { get; set; }
}

// Create a dictionary
var dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonStr);

// Create a list of Fields
List<Field> fields = dict.Select(x => new Field() { Name = x.Key, Value = x.Value }).ToList();

// Create the final Object.
RootObject rootObj = JsonConvert.SerializeObject(new RootObject() { Fields = fields });
公共类根对象
{
[JsonProperty(“字段”)]
公共列表字段{get;set;}
}
公共类字段
{
[JsonProperty(“名称”)]
公共字符串名称{get;set;}
[JsonProperty(“价值”)]
公共字符串值{get;set;}
}
//创建字典
var dict=JsonConvert.DeserializeObject(jsonStr);
//创建字段列表
List fields=dict.Select(x=>newfield(){Name=x.Key,Value=x.Value}).ToList();
//创建最终对象。
RootObject rootObj=JsonConvert.SerializeObject(新的RootObject(){Fields=Fields});

替代解决方案,使用
JObject.Parse()
解析原始JSON,然后迭代其属性以创建具有不同名称和值的JObject数组。
然后将生成的
IEnumerable
转换为JArray,用于创建最终的
字段
对象

var jObj = JObject.Parse(json);

var newObjects = jObj.Properties().Select(p => new JObject {
    new JProperty("name", p.Name),
    new JProperty("value", p.Value)});

var fields = new JObject() {
    { "fields",  JArray.FromObject(newObjects)}
};

Console.WriteLine(fields);

将其反序列化为字典,将其转换为列表,然后序列化。
var jObj = JObject.Parse(json);

var newObjects = jObj.Properties().Select(p => new JObject {
    new JProperty("name", p.Name),
    new JProperty("value", p.Value)});

var fields = new JObject() {
    { "fields",  JArray.FromObject(newObjects)}
};

Console.WriteLine(fields);