Web.Contents方法将Content作为二进制
我使用此代码。有用
query = "{
""field1"" : ""value1"",
""field2"" : ""value2"",
""field3"" : {
""sub_field_3_1"" : [""value_3_1_1"", ""value_3_1_2"", ""value_3_1_1""],
""sub_field_3_2"" : [""value_3_2_1"", ""value_3_2_2"", ""value_3_2_1""]
}
}",
content = Text.ToBinary(query),
Web.Contents("https://my_url", [
Headers = [#"Content-Type"="text/xml; charset=utf-8"],
Content=content
])
我知道,这不是一个好的解决方法,因为没有理由进行双重转换。但是我找不到一种方法来应用记录,它应该看起来像这样:
record = [
field1 = value1,
field2 = value2,
field3 = [
sub_field_3_1 = {value_3_1_1, value_3_1_2, value_3_1_1},
sub_field_3_2 = {value_3_2_1, value_3_2_2, value_3_2_1}
]
],
content = SOME_CONVERTER(record),
Web.Contents("https://my_url", [
Headers = [#"Content-Type"="text/xml; charset=utf-8"],
Content = content
])
试图使用Uri.BuildQueryString(如何使用Power Query的Web.Contents来发布多部分/表单数据),但是它不能正确地形成Binary
record = [
field1 = value1,
field2 = value2,
field3 = [
sub_field_3_1 = {value_3_1_1, value_3_1_2, value_3_1_1},
sub_field_3_2 = {value_3_2_1, value_3_2_2, value_3_2_1}
]
],
content = Text.ToBinary(Uri.BuildQueryString(record)),
Web.Contents("https://my_url", [
Headers = [#"Content-Type"="text/xml; charset=utf-8"],
Content=content
]
有更好的解决方法吗?
目前,您的硬编码JSON字符串是更好的解决方案之一。
这并不理想,但是您可以使用自己的值到JSON转换功能,例如toJson
:
let
record = [
field1 = "value1",
field2 = "value2",
field3 = [
sub_field_3_1 = {"value_3_1_1", null, 3.2},
sub_field_3_2 = {"value_3_2_1", "value_3_2_2", "value_3_2_1"}
]
],
toJson = (v as any) as text =>
if v is null then "null" else
if v is logical or v is number then Text.From(v) else
if v is text then """" & Text.Replace(Text.Replace(v, "\", "\\"), """", "\""") & """" else
if v is list then "[" & Text.Combine(List.Transform(v, @toJson), ", ") & "]" else
if v is record then "{" &
Text.Combine(List.Transform(
Record.FieldNames(v),
(n) => @toJson(n) & ": " & @toJson(Record.Field(v, n))), ", ")
& "}" else
error "not implemented",
jsonText = toJson(record)
in
jsonText
与实际Json.FromValue
库函数应做的工作相比,存在一些缺陷:
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句