【发布时间】:2021-07-02 05:33:58
【问题描述】:
我正在调用 Web 服务并获得以下 Json 请求
{"handler":{"name":"abc"},"intent":{"name":"actions.intent.MAIN","params":{},"query":"Mit Google sprechen"},"scene":{"name":"actions.scene.START_CONVERSATION","slotFillingStatus":"UNSPECIFIED","slots":{},"next":{"name":"Start_Frage"}},"session":{"id":"ABwppHHVumDrliLJaLSikS6KnIlN7yYv6Z4XJCOYzEZt8Fr08RH6r0wtM2-E0v40lS2p1YosTDfpSCd5Lw","params":{},"typeOverrides":[],"languageCode":""},"user":{"locale":"de-DE","params":{},"accountLinkingStatus":"ACCOUNT_LINKING_STATUS_UNSPECIFIED","verificationStatus":"VERIFIED","packageEntitlements":[],"gaiamint":"","permissions":[],"lastSeenTime":"2021-04-01T10:06:59Z"},"home":{"params":{}},"device":{"capabilities":["SPEECH","RICH_RESPONSE","LONG_FORM_AUDIO"]}}
我使用https://json2csharp.com/ 将我的 Json 字符串转换为 C# 类
public class Handler
{
public string name { get; set; }
}
public class Params
{
}
public class Intent
{
public string name { get; set; }
public Params @params { get; set; }
public string query { get; set; }
}
public class Slots
{
}
public class Next
{
public string name { get; set; }
}
public class Scene
{
public string name { get; set; }
public string slotFillingStatus { get; set; }
public Slots slots { get; set; }
public Next next { get; set; }
}
public class Session
{
public string id { get; set; }
public Params @params { get; set; }
public List<object> typeOverrides { get; set; }
public string languageCode { get; set; }
}
public class User
{
public string locale { get; set; }
public Params @params { get; set; }
public string accountLinkingStatus { get; set; }
public string verificationStatus { get; set; }
public List<object> packageEntitlements { get; set; }
public string gaiamint { get; set; }
public List<object> permissions { get; set; }
public DateTime lastSeenTime { get; set; }
}
public class Home
{
public Params @params { get; set; }
}
public class Device
{
public List<string> capabilities { get; set; }
}
public class RequestJson
{
public Handler handler { get; set; }
public Intent intent { get; set; }
public Scene scene { get; set; }
public Session session { get; set; }
public User user { get; set; }
public Home home { get; set; }
public Device device { get; set; }
}
我需要发回以这种方式格式化的 JSON 响应
{"responseJson": {"session": {"id": "ABwppHF4mhTR8RAjnFSt7me_NFR1hRKHY5N6X0kfONIcz_VVPUOmR8VddWVJ3GM3G8ix3OR3O1Ew2wbldc5cRziYebVA","params": {},"languageCode": ""},"prompt": {"override": false,"firstSimple": {"speech": "Stellen Sie eine Frage","text": ""}}}}
https://json2csharp.com/ 生成此对象结构
public class Params { } public class Session { public string id { get; set; } public Params @params { get; set; } public string languageCode { get; set; } } public class FirstSimple { public string speech { get; set; } public string text { get; set; } } public class Prompt { public bool @override { get; set; } public FirstSimple firstSimple { get; set; } } public class ResponseJson { public Session session { get; set; } public Prompt prompt { get; set; } } }
我当前的代码如下所示: 如果我这样做,我会得到一个 nullreferenceexception。我无法在 RespondJson 对象中设置任何值。我是编程新手。非常感谢您的帮助
[HttpPost]
public async Task<IActionResult> PostWebHook([FromBody] RequestJson request)
{
ResponseJson response = new ResponseJson();
string body;
using (var reader = new StreamReader(Request.Body))
{
body = await reader.ReadToEndAsync();
// => doesnt work that way I get a nullreferenceexception
response.session.id = request.session.id; //nullreferenceexception
response.prompt.@override = false;
response.prompt.firstSimple.speech = "Test123";
//
}
return Ok(response);
}
}
我该怎么做? 简而言之:我收到一个 JSON 格式的请求,但稍后我需要以另一种 JSON 格式发回响应。
【问题讨论】:
-
不清楚,有什么问题。通常可以有不同的输入和输出模型
-
更新了我的代码,如果我尝试这样做,我会得到一个 nullreferenceexception。我是新手和编程新手。感谢您的帮助
-
您必须先阅读请求模型,然后再写入响应模型
-
所以你的请求是空的吗?
-
我收到一个请求 Json,其中包含已预填充的值。从这个请求 Json 对象中,我需要获取 ID 值,并将其设置在我的响应对象中。 (就像在我的代码中一样,但我无法设置值,因为我得到了 nullreferenceexception)
标签: c# asp.net json json.net deserialization