第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何將 Flutter iso8601 DateTime 解碼為 Go Api RFC3339

如何將 Flutter iso8601 DateTime 解碼為 Go Api RFC3339

Go
慕哥6287543 2022-11-28 10:20:30
在我的 Go API 中,我試圖使用 json 解碼來(lái)解析以下 json。{"contract_id":0,"date_established":"2022-04-03T00:00:00.000","expiry_date":null,"extension_expiry_date":null,"description":"fffff"}我得到一個(gè)錯(cuò)誤:parsing time "\"2022-04-03T00:00:00.000\"" as "\"2006-01-02T15:04:05Z07:00\"": cannot parse "\"" as "Z07:00"我該如何解決這個(gè)錯(cuò)誤?這是我的結(jié)構(gòu):// Contract modeltype Contract struct {    ContractId          *int       `json:"contract_id"`    CompanyId           *int       `json:"company_id"`    DateEstablished     *time.Time `json:"date_established"`    ExpiryDate          *time.Time `json:"expiry_date"`    ExtensionExpiryDate *time.Time `json:"extension_expiry_date"`    Description         *string    `json:"description"`}這是我的代碼:func (rs *appResource) contractCreate(w http.ResponseWriter, r *http.Request) {    var contract Contract    decoder := json.NewDecoder(r.Body)    err = decoder.Decode(&contract)
查看完整描述

2 回答

?
慕的地6264312

TA貢獻(xiàn)1817條經(jīng)驗(yàn) 獲得超6個(gè)贊

Go 使用 RFC 3339 編碼時(shí)間,如果你控制正在生成的 json,你只需要更改2022-04-03T00:00:00.000為2022-04-03T00:00:00.000Z.


例如,這有效。


type Contract struct {

    ContractId          *int       `json:"contract_id"`

    CompanyId           *int       `json:"company_id"`

    DateEstablished     *time.Time `json:"date_established"`

    ExpiryDate          *time.Time `json:"expiry_date"`

    ExtensionExpiryDate *time.Time `json:"extension_expiry_date"`

    Description         *string    `json:"description"`

}


func main() {

    body := `{"contract_id":0,"date_established":"2022-04-03T00:00:00.000Z","expiry_date":null,"extension_expiry_date":null,"description":"fffff"}`


    var contract Contract

    reader := strings.NewReader(body)

    decoder := json.NewDecoder(reader)

    err := decoder.Decode(&contract)

    if err != nil {

        fmt.Println("Error: ", err)

    } else {

        fmt.Printf("Contract: %+v\n", contract)

    }

}

如果你不控制json,你需要寫(xiě)一個(gè)自定義的解組方法。



查看完整回答
反對(duì) 回復(fù) 2022-11-28
?
LEATH

TA貢獻(xiàn)1936條經(jīng)驗(yàn) 獲得超7個(gè)贊

這是我最終在 Flutter 中使用我的 Go api 和列類(lèi)型以及在 Postgresql 中實(shí)現(xiàn)pgtype.Date的pgtype.DateTimestamptz解決DATE方案DATETIME。我無(wú)法使用具有這種數(shù)據(jù)類(lèi)型的指針。


首先,我在 Flutter 中創(chuàng)建了一個(gè)這樣的助手:


import 'package:intl/date_symbol_data_local.dart';

import 'package:intl/intl.dart';


extension DateTimeExtension on DateTime {

  String format([String pattern = 'dd/MM/yyyy', String? locale]) {

    if (locale != null && locale.isNotEmpty) {

      initializeDateFormatting(locale);

    }

    return DateFormat(pattern, locale).format(this);

  }


  String formatDbDateTime(

      [String pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", String? locale]) {

    if (locale != null && locale.isNotEmpty) {

      initializeDateFormatting(locale);

    }

    return DateFormat(pattern, locale).format(this);

  }


  String formatDbDate([String pattern = 'yyyy-MM-dd', String? locale]) {

    if (locale != null && locale.isNotEmpty) {

      initializeDateFormatting(locale);

    }

    return DateFormat(pattern, locale).format(this);

  }


  String formatLocalDate([String pattern = 'dd MMMM yyyy', String? locale]) {

    if (locale != null && locale.isNotEmpty) {

      initializeDateFormatting(locale);

    }

    return DateFormat(pattern, locale).format(this);

  }


  String formatLocalDateTime(

      [String pattern = 'dd MMMM yyyy h:mm a', String? locale]) {

    if (locale != null && locale.isNotEmpty) {

      initializeDateFormatting(locale);

    }

    return DateFormat(pattern, locale).format(this);

  }

}

我在我的 Flutter 模型中使用它來(lái)格式化我的 Go Api 的日期:


if (planAcceptedDate != null) {

  data['plan_accepted_date'] = planAcceptedDate!.formatDbDateTime();

} else {

  data['plan_accepted_date'] = null;

}


if (invoiceDate != null) {

  data['invoice_date'] = invoiceDate!.formatDbDate();

} else {

  data['invoice_date'] = null;

}

在我的 Flutter 視圖表單中,我這樣使用它:


        Container(

          padding: EdgeInsets.all(8.0),

          child: Text(

            _matter.planAcceptedDate == null

                ? ''

                : _matter.planAcceptedDate!.formatLocalDateTime(),

            style: TextStyle(

              color: appTextColor,

              fontWeight: FontWeight.normal,

              fontSize: _user.fontsize,

            ),

          ),

        ),

        Container(

          padding: EdgeInsets.all(8.0),

          child: Text(

            _matter.invoiceDate == null

                ? ''

                : _matter.invoiceDate!.formatLocalDate(),

            style: TextStyle(

              color: appTextColor,

              fontWeight: FontWeight.normal,

              fontSize: _user.fontsize,

            ),

          ),

        ),

我的 Go 結(jié)構(gòu)如下所示:


PlanAcceptedDate pgtype.Timestamptz `json:"plan_accepted_date"`

InvoiceDate      pgtype.Date        `json:"invoice_date"`


查看完整回答
反對(duì) 回復(fù) 2022-11-28
  • 2 回答
  • 0 關(guān)注
  • 152 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)