簡體   English   中英

將 JSON 請求轉換為 C# object 並以不同的 Z0ECD11C1D7A287401D148A23BBD 格式發送回響應

[英]Convert JSON Request to C# object and send Response back in different JSON format

我正在調用網絡服務並關注 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/導致這個 object 結構

 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 object 中設置任何值。 我是編程新手。 我將非常感謝您的幫助

[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 格式發回響應。

最好的方法是有兩個獨立的模型。 一個預請求,每個響應第二個。 當然,您可以在兩個模型中重用一些類。 In your json you can reuse the session object and then you have to create new class which will be represent prompt object. 您的響應 object 示例:

  public class Params
    {
    }

    public class Session
    {
        [JsonPropertyName("id")]
        public string Id { get; set; }

        [JsonPropertyName("params")]
        public Params Params { get; set; }

        [JsonPropertyName("languageCode")]
        public string LanguageCode { get; set; }
    }

    public class FirstSimple
    {
        [JsonPropertyName("speech")]
        public string Speech { get; set; }

        [JsonPropertyName("text")]
        public string Text { get; set; }
    }

    public class Prompt
    {
        [JsonPropertyName("override")]
        public bool Override { get; set; }

        [JsonPropertyName("firstSimple")]
        public FirstSimple FirstSimple { get; set; }
    }

    public class ResponseJson
    {
        [JsonPropertyName("session")]
        public Session Session { get; set; }

        [JsonPropertyName("prompt")]
        public Prompt Prompt { get; set; }
    }

    public class Response
    {
        [JsonPropertyName("responseJson")]
        public ResponseJson ResponseJson { get; set; }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM