使用 RestSharp 更正 JSON 结构
Correct JSON structure using RestSharp
使用 RestSharp 发送 JSON 的正确格式是什么:
Example PUT JSON:
{
"properties": [
{
"name": "description",
"value": "A far better description than before"
}
]
}
在 C# 中如何正确发送,我正在尝试:
request.AddJsonBody(new
{
properties = new[]
{
new{property="name",value="about_us"},
new{property="value",value="My description"}
}
});
完整代码如下:
private void UpdateCompanyProperty(string companyId)
{
var hapikey = "{YOUR_HAPI_KEY_HERE}";
var client = new RestClient("https://api.hubapi.com/");
var request = new RestRequest("companies/v2/companies/{companyId}", Method.PUT);
request.AddUrlSegment("companyId", companyId);
request.AddQueryParameter("hapikey", hapikey);
request.RequestFormat = DataFormat.Json;
request.AddJsonBody(new
{
properties = new[]
{
new{property="name",value="about_us"},
new{property="value",value="My description"}
}
});
IRestResponse response = client.Execute(request);
JObject jObject = JObject.Parse(response.Content);
JToken jvid = jObject["portalId"];
Debug.WriteLine(jvid);
}
没有错误,但没有更新或返回值。
在这里试试我的回答:
request.RequestFormat = DataFormat.Json; // Important
var input = new Dictionary<string, object>();
// props could be an array or real objects too of course
var props = new[]
{
new{property="name",value="about_us"},
new{property="value",value="My description"}
};
input.Add("properties", props);
request.AddBody(input);
创建一个 class 并给它任意名称
class MyClass
{
public string property {get;set;}
private string value {get;set;}
}
将您的 class 定义为对象
List<MyClass> list = new List<MyClass>
{
new MyClass() { property = "name", value = "about_us"},
new MyClass() { property = "value", value = "My Description"},
};
现在使用 Newtonsoft.Json 序列化您的对象
字符串结果=JsonConvert.SerializeObject(列表);
现在将其添加到数组中
var resArray = new object[] { result };
在下面找到您修改后的代码
class MyClass
{
public string property {get;set;}
private string value {get;set;}
}
using Newtonsoft.Json;
using RestSharp;
private void UpdateCompanyProperty(string companyId)
{
List<MyClass> list = new List<MyClass>
{
new MyClass() { property = "name", value = "about_us"},
new MyClass() { property = "value", value = "My Description"},
};
string result = JsonConvert.SerializeObject(list);
var hapikey = "{YOUR_HAPI_KEY_HERE}";
var client = new RestClient("https://api.hubapi.com/");
var request = new RestRequest("companies/v2/companies/{companyId}", Method.PUT);
request.AddUrlSegment("companyId", companyId);
request.AddQueryParameter("hapikey", hapikey);
request.RequestFormat = DataFormat.Json;
request.AddJsonBody(new
{
properties =result
});
IRestResponse response = client.Execute(request);
JObject jObject = JObject.Parse(response.Content);
JToken jvid = jObject["portalId"];
Debug.WriteLine(jvid);
}
使用 RestSharp 发送 JSON 的正确格式是什么:
Example PUT JSON:
{
"properties": [
{
"name": "description",
"value": "A far better description than before"
}
]
}
在 C# 中如何正确发送,我正在尝试:
request.AddJsonBody(new
{
properties = new[]
{
new{property="name",value="about_us"},
new{property="value",value="My description"}
}
});
完整代码如下:
private void UpdateCompanyProperty(string companyId)
{
var hapikey = "{YOUR_HAPI_KEY_HERE}";
var client = new RestClient("https://api.hubapi.com/");
var request = new RestRequest("companies/v2/companies/{companyId}", Method.PUT);
request.AddUrlSegment("companyId", companyId);
request.AddQueryParameter("hapikey", hapikey);
request.RequestFormat = DataFormat.Json;
request.AddJsonBody(new
{
properties = new[]
{
new{property="name",value="about_us"},
new{property="value",value="My description"}
}
});
IRestResponse response = client.Execute(request);
JObject jObject = JObject.Parse(response.Content);
JToken jvid = jObject["portalId"];
Debug.WriteLine(jvid);
}
没有错误,但没有更新或返回值。
在这里试试我的回答:
request.RequestFormat = DataFormat.Json; // Important
var input = new Dictionary<string, object>();
// props could be an array or real objects too of course
var props = new[]
{
new{property="name",value="about_us"},
new{property="value",value="My description"}
};
input.Add("properties", props);
request.AddBody(input);
创建一个 class 并给它任意名称
class MyClass
{
public string property {get;set;}
private string value {get;set;}
}
将您的 class 定义为对象
List<MyClass> list = new List<MyClass>
{
new MyClass() { property = "name", value = "about_us"},
new MyClass() { property = "value", value = "My Description"},
};
现在使用 Newtonsoft.Json 序列化您的对象
字符串结果=JsonConvert.SerializeObject(列表);
现在将其添加到数组中
var resArray = new object[] { result };
在下面找到您修改后的代码
class MyClass
{
public string property {get;set;}
private string value {get;set;}
}
using Newtonsoft.Json;
using RestSharp;
private void UpdateCompanyProperty(string companyId)
{
List<MyClass> list = new List<MyClass>
{
new MyClass() { property = "name", value = "about_us"},
new MyClass() { property = "value", value = "My Description"},
};
string result = JsonConvert.SerializeObject(list);
var hapikey = "{YOUR_HAPI_KEY_HERE}";
var client = new RestClient("https://api.hubapi.com/");
var request = new RestRequest("companies/v2/companies/{companyId}", Method.PUT);
request.AddUrlSegment("companyId", companyId);
request.AddQueryParameter("hapikey", hapikey);
request.RequestFormat = DataFormat.Json;
request.AddJsonBody(new
{
properties =result
});
IRestResponse response = client.Execute(request);
JObject jObject = JObject.Parse(response.Content);
JToken jvid = jObject["portalId"];
Debug.WriteLine(jvid);
}