作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我在访问使用 StringContent 生成的 HttpResponseMessage 中的 JSON 对象时遇到问题。
JSON 对象如下所示:
{
"object": "JOB",
"entry": [
{
"uuid": "1nufisnfiu3-dn3u2irb-dn3ui2fb9-nui2",
"changed_fields": [
"status"
],
"time": "2018-09-30 21:57:02"
}
],
"resource_url": "https://somewebsiteAPI.com/api_1.0/Jobs/1nufisnfiu3-dn3u2irb-dn3ui2fb9-nui2.json"
}
这是我的 Controller :
[HttpPost]
public async Task<HttpResponseMessage> Post()
{
string result = await Request.Content.ReadAsStringAsync();
var resp = new HttpResponseMessage(HttpStatusCode.OK);
resp.Content = new StringContent(result, System.Text.Encoding.UTF8, "application/json");
return resp;
}
我的目标是获取 resource_url json 对象。
如有任何帮助,我们将不胜感激!谢谢!
最佳答案
需要让JSON
字符串为对象,然后获取resource_url
值。
我会使用 Json.net做到这一点。
看起来像这样。
public class Entry
{
public string uuid { get; set; }
public List<string> changed_fields { get; set; }
public string time { get; set; }
}
public class RootObject
{
public List<Entry> entry { get; set; }
public string resource_url { get; set; }
}
JsonConvert.DeserializeObject
方法让JSON
字符串建模。 然后获取模型的 resource_url
属性并将其作为参数传递到 StringContent
中。
[HttpPost]
public async Task<HttpResponseMessage> Post()
{
string result = await Request.Content.ReadAsStringAsync();
var resp = new HttpResponseMessage(HttpStatusCode.OK);
var model = JsonConvert.DeserializeObject<RootObject>(result);
resp.Content = new StringContent(model.resource_url, System.Text.Encoding.UTF8, "application/json");
return resp;
}
关于c# - 使用 StringContent 访问 HttpResponseMessge 中的 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52582788/
我在访问使用 StringContent 生成的 HttpResponseMessage 中的 JSON 对象时遇到问题。 JSON 对象如下所示: { "object": "JOB",
我是一名优秀的程序员,十分优秀!