在 json 文件中我有此文本
"result": "1:3",
"points": "29:37",
现在我想用 powershell 将其更改为
"result": "1-3",
"points": "29-37",
怎么做?我想我需要某种正则表达式来完成它。
在 json 文件中我有此文本
"result": "1:3",
"points": "29:37",
现在我想用 powershell 将其更改为
"result": "1-3",
"points": "29-37",
怎么做?我想我需要某种正则表达式来完成它。
你好,我正在尝试将 nestedJson 检索到 dart,主要我将使用具有类似数据嵌套 Json 的 API 调用,因此下面是我尝试的示例,看看它是否有效。所以我的主要问题是我没有得到预期的输出。下面提到的是我的代码文件,如果这看起来是错误的或乏味的,请指导我正确的方法。
预期产出: | 我得到的输出: |
---|---|
达斯塔吉尔 | 达斯塔吉尔 |
三十四 | 三十四 |
上海 | 上海 |
[] | [] |
============ | ============ |
中国 | |
孟加拉国 |
main.dart
void main() {
var myMap = {
"name": "dastagir",
"age": 34,
"city":"Shangai",
"address": [
{ // Map 1
"country": "china",
"city": "Shanghai"
},
{ // Map 2
"country": "Bangaldesh",
"city": "Dhaka"
},
]
};
var obj = Person.fromJson(myMap);
print(obj.name);
print(obj.age);
print(obj.city);
print(obj.address);
print("=======================================");
var myAddress = obj.address;
myAddress!.map((e){
print(e.country);
}).toList();
}
nested_json.dart
class Person{
String? name;
int? age;
String? city;
List<Address>? address;
Person({this.name, this.age, this.city, this.address});
Person.fromJson(Map<String, dynamic> json){
name = json['name'];
age = json['age'];
city = json['city'];
if(json['address']!=null){
address=<Address>[];
}
else{
(json['address'] as List).forEach((e){
address!.add(Address.fromJson(e));
});
}
}
}
// this is for inner Json List
class Address{
String? country;
String? city;
Address({this.country, this.city});
Address.fromJson(Map<String, dynamic> json){
country = json['country'];
city = json['city'];
}
}`
我正在使用一个 Go 应用程序,需要将原始 JSON 数据传递给结构字段,但在编组结构时遇到了额外转义的问题,尤其是当原始 JSON 包含空格和制表符时。具体来说,我使用以下结构:
type BrandTemplate struct {
Type string `json:"type"` // Template type (e.g., email_forgot_password)
Locale string `json:"locale"` // Locale (e.g., "es" for Spanish, "en" for English)
Template string `json:"template"` // Template content (Email/SMS content)
}
type BrandTemplate1 struct {
Type string `json:"type"`
Locale string `json:"locale"`
Template json.RawMessage `json:"template"` // Use RawMessage to avoid extra escaping
}
该BrandTemplate
结构期望模板字段是一个字符串,但我需要传递的原始 JSON 包含空格、制表符和需要转义的特殊字符,而我不想手动转义。
另一方面,BrandTemplate1
结构体使用json.RawMessage
,它工作得很好,因为它保留了原始 JSON 格式。但我想使用BrandTemplate
结构体,我想知道是否有办法避免手动转义 JSON 字符串并以干净的方式传递原始 JSON,同时保留空格、制表符和其他格式。
下面是我正在尝试做的一个例子:
// Define the template as raw JSON
template := `{
"subject": "Email MFA App Verification Code",
"html": "<html><head></head><body><p>Here is the code: {{otp_code}}</p></body></html>",
"plain": "Here is the code: {{otp_code}}"
}`
// Create a new BrandTemplate object
brandTemplate := models.BrandTemplate{
Type: "email_code_app_verification", // Example template type
Locale: "en", // Locale for the template (English)
Template: template, // Pass the raw JSON string
}
这是我打印时得到的输出brandTemplate
:
BrandTemplate JSON:
{
"type": "email_code_app_verification",
"locale": "en",
"template": "{\n\t\t\"subject\": \"Email MFA App Verification Code\",\n\t\t\"html\": \"\\u003chtml\\u003e\\u003chead\\u003e\\u003c/head\\u003e\\u003cbody\\u003e\\u003cp\\u003eHere is the code: {{otp_code}}\\u003c/p\\u003e\\u003c/body\\u003e\\u003c/html\\u003e\",\n\t\t\"plain\": \"Here is the code: {{otp_code}}\"\n\t}"
}
这是预期的输出:
BrandTemplate JSON:
{
"type": "email_code_app_verification",
"locale": "en",
"template": {
"subject": "Email MFA App Verification Code",
"html": "<html><head></head><body><p>Here is the code: {{otp_code}}</p></body></html>",
"plain": "Here is the code: {{otp_code}}"
}
}
有没有更好的方法可以将带有空格和制表符的原始 JSON 传递到BrandTemplate
结构中而无需进行额外的转义或编组?我曾json.RawMessage
在其他情况下看到过这种用法,但如果可能的话,我想避免这种情况并直接使用BrandTemplate
结构。
任何关于此问题的建议或解决方案都将不胜感激!
我有以下代表数据库模式的 JSON 结构:
{
"db": [
{
"tables": [
{
"name": "tblFoo",
"cols": [
{
"name": "created",
"types": [ { "data_type": "timestamp" } ]
},
{
"name": "updated",
"types": [ { "data_type": "timestamp" } ]
},
{
"name": "username",
"types": [ { "data_type": "timestamp" } ]
}
]
},
{
"name": "tblBar",
"cols": [
{
"name": "created",
"types": [ { "data_type": "timestamp" } ]
},
…
等等,你明白了。我需要将其转换为一组连续的 json“命令”,其中每个表及其列都是单独的一行,如下所示:
[
{"type": "TABLE", name:"tblFoo"},
{"type": "COLUMN", name:"created", ofTable: "tblFoo"},
{"type": "COLUMN", name:"updated", ofTable: "tblFoo"},
…
{"type": "TABLE", name:"tblBar"},
{"type": "COLUMN", name:"created", ofTable: "tblBar"},
]
我就是想不通。我.db.tables[] | map(…)
一开始尝试过,但这显然只会创建第一级表格的数组,我无法将列定义添加/注入到同一级。
然后,我尝试使用递归运算符等对列进行迭代.db.tables[].cols[]
,但结果列表仍然只是列的列表,并且我无法将表的列表添加/注入到传出列表中。
你能帮助我走上正确的道路吗?
我在 Snowflake 中有一张如下所示的表格:
+------+------+------+------------------------------------------------------------------+
| Col1 | Col2 | Col3 | Col4 |
+------+------+------+------------------------------------------------------------------+
| 1 | foo | 32 | [{"id":"1", "category":"black"}] |
| 2 | bar | 22 | [{"id":"1", "category":"black"}, {"id":"4", "category":"white"}] |
| 3 | smeg | 2 | null |
+------+------+------+------------------------------------------------------------------+
我想将数据转换到另一个表中,如下所示:
+------+------+------+------+----------+
| Col1 | Col2 | Col3 | id | category |
+------+------+------+------+----------+
| 1 | foo | 32 | 1 | black |
| 2 | bar | 22 | 1 | black |
| 2 | bar | 22 | 4 | white |
| 3 | smeg | 2 | null | null |
+------+------+------+------+----------+
我之前使用过 SQL Server 中完成此操作JSON_VALUE
,OPENJSON
但是在尝试寻找 Snowflake 等效项时遇到了困难。
我有两种互相矛盾的心态:
JSON 数字始终是双精度浮点数。因此:
1
和之间没有语义差异1.0
- 都代表完全相同的数字;12345678901234567890
其实是12345678901234567000
因为12345678901234567890
无法准确表示为双精度浮点数。JSON 数字不能总是被解释为双精度浮点数。JSON 是一种不同于 JavaScript 的通信协议。认为 JSON 数字总是双精度浮点数的观点源于 JavaScript 和 JSON 之间的混淆,以及 JavaScript 中默认 JSON 解析器和序列化器的特性,它们以这种方式解释它们。因此:
1
和1.0
不必相同。具体来说,尾随的有.0
无可用于编码类型信息。许多编程语言(如 Java 或 C#)区分整数和浮点数。要求这些语言中的整数必须始终不带尾随 进行序列化.0
,而浮点数必须始终带尾随 进行序列化.0
,这是合理的。12345678901234567890
和12345678901234567000
不是相同的数字。某些广泛使用的解析器默认将它们解释为相同的数字,因为它们将 JSON 数字强制转换为双精度浮点数 - 但这是这些解析器的问题,而不是 JSON 本身的问题。这两种心态,如果有的话,哪一种是正确的呢?
谷歌搜索似乎得出了相互矛盾的结果。
我知道这是新手才会说的话,但它真的让我难以忍受!
这是我当前的代码:
func WeatherAPI() async throws {
let parameters = [
"location": "Greenvale Victoria",
"fields": ["temperature"],
"units": "metric",
"timesteps": ["1h"],
"startTime": "2025-03-03T00:00:00+11:00",
"endTime": "nowPlus24h",
"dailyStartHour": 6,
"timezone": "Australia/Melbourne"
] as [String : Any?]
let postData = try JSONSerialization.data(withJSONObject: parameters, options: [])
let url = URL(string: "https://api.tomorrow.io/v4/timelines")!
var components = URLComponents(url: url, resolvingAgainstBaseURL: true)!
let queryItems: [URLQueryItem] = [
URLQueryItem(name: "apikey", value: "This ain't for you!"),
]
components.queryItems = components.queryItems.map { $0 + queryItems } ?? queryItems
var request = URLRequest(url: components.url!)
request.httpMethod = "POST"
request.timeoutInterval = 10
request.allHTTPHeaderFields = [
"accept": "application/json",
"Accept-Encoding": "deflate, gzip, br",
"content-type": "application/json"
]
request.httpBody = postData
let (data, temperature) = try await URLSession.shared.data(for: request)
print(String(decoding: data, as: UTF8.self))
}
最后的打印语句返回以下内容:
{"data":{"timelines":[{"timestep":"1h","endTime":"2025-03-04T13:00:00+11:00","startTime":"2025-03-03T00:00:00+11:00","intervals":[{"startTime":"2025-03-03T00:00:00+11:00","values":{"temperature":14.3}},{"startTime":"2025-03-03T01:00:00+11:00","values":{"temperature":13.9}},{"startTime":"2025-03-03T02:00:00+11:00","values":{"temperature":13.4}},{"startTime":"2025-03-03T03:00:00+11:00","values":{"temperature":13.1}},{"startTime":"2025-03-03T04:00:00+11:00","values":{"temperature":12.7}},{"startTime":"2025-03-03T05:00:00+11:00","values":{"temperature":12.5}},{"startTime":"2025-03-03T06:00:00+11:00","values":{"temperature":11.6}},{"startTime":"2025-03-03T07:00:00+11:00","values":{"temperature":11.3}},{"startTime":"2025-03-03T08:00:00+11:00","values":{"temperature":12.3}},{"startTime":"2025-03-03T09:00:00+11:00","values":{"temperature":14.4}},{"startTime":"2025-03-03T10:00:00+11:00","values":{"temperature":16.4}},{"startTime":"2025-03-03T11:00:00+11:00","values":{"temperature":17.6}},{"startTime":"2025-03-03T12:00:00+11:00","values":{"temperature":18.8}},{"startTime":"2025-03-03T13:00:00+11:00","values":{"temperature":19.7}},{"startTime":"2025-03-03T14:00:00+11:00","values":{"temperature":20.2}},{"startTime":"2025-03-03T15:00:00+11:00","values":{"temperature":20}},{"startTime":"2025-03-03T16:00:00+11:00","values":{"temperature":19.6}},{"startTime":"2025-03-03T17:00:00+11:00","values":{"temperature":19}},{"startTime":"2025-03-03T18:00:00+11:00","values":{"temperature":18.1}},{"startTime":"2025-03-03T19:00:00+11:00","values":{"temperature":17}},{"startTime":"2025-03-03T20:00:00+11:00","values":{"temperature":15.9}},{"startTime":"2025-03-03T21:00:00+11:00","values":{"temperature":15.3}},{"startTime":"2025-03-03T22:00:00+11:00","values":{"temperature":14.7}},{"startTime":"2025-03-03T23:00:00+11:00","values":{"temperature":14.2}},{"startTime":"2025-03-04T00:00:00+11:00","values":{"temperature":13.7}},{"startTime":"2025-03-04T01:00:00+11:00","values":{"temperature":13.3}},{"startTime":"2025-03-04T02:00:00+11:00","values":{"temperature":13.1}},{"startTime":"2025-03-04T03:00:00+11:00","values":{"temperature":12.7}},{"startTime":"2025-03-04T04:00:00+11:00","values":{"temperature":12.3}},{"startTime":"2025-03-04T05:00:00+11:00","values":{"temperature":11.9}},{"startTime":"2025-03-04T06:00:00+11:00","values":{"temperature":11.5}},{"startTime":"2025-03-04T07:00:00+11:00","values":{"temperature":11.2}},{"startTime":"2025-03-04T08:00:00+11:00","values":{"temperature":12.3}},{"startTime":"2025-03-04T09:00:00+11:00","values":{"temperature":14.9}},{"startTime":"2025-03-04T10:00:00+11:00","values":{"temperature":17.3}},{"startTime":"2025-03-04T11:00:00+11:00","values":{"temperature":20.6}},{"startTime":"2025-03-04T12:00:00+11:00","values":{"temperature":24.8}},{"startTime":"2025-03-04T13:00:00+11:00","values":{"temperature":28}}]}]}}
尽管它返回了我要求的数据,但我只想要数字,而不是其他垃圾。理想情况下,我只想得到类似
if let value in data = APIDATA {
print(value[1]) /// the first temperature recorded
print(value[2]) /// the second temperature recorded
print(value[3]) /// the third temperature recorded
}
早上好,
我正在尝试将 polars df 转换为 json 对象(在 python 中),但似乎找不到在行/列方向之间更改其格式的方法。在 Pandas 中,默认情况下,它会创建一个面向列的 JSON 对象,并且可以轻松更改它,但在 Polars 中它是面向行的,我似乎找不到更改它的方法,因为write_json
如果您想直接保存它,该方法实际上不会接收除目标文件名之外的任何参数。
知道如何实现吗?
提前致谢!
我正在尝试将包含几千行的 JSON 文件中的数据输出到 CSV。JSON 组件遵循以下一般模式:
{
"name": "pickles",
"aliases": [
"birds"
]
}
{
"name": "cheese",
"aliases": [
"cheese1",
"cheese2"
]
}
有些别名有多个值(最多四个),有些只有一个值。我希望 CSV 输出有两列 - 一列用于名称,一列用于别名,其中名称中的值将位于名称列中,别名的值将位于别名列中(见下文)。
name,aliases
pickles,birds
cheese, cheese1
cheese, cheese2
我正在尝试弄清楚使用哪个 jq 命令来获取此输出,但我被错误“无法使用字符串“name”索引字符串”困住了
我尝试了一些 jq 命令,但遇到了类似的错误。最近我尝试了:
jq -r '.[] | . as {$name} | [$name, .name, .aliases] | @csv' filename.json
我正在尝试通过客户端 xmlprc 库创建日志注释,方法是通过帮助台模块中的 json 文件读取日志注释,但却遇到了名为“位置参数不符合预期”的 typeError。错误如下:-
TypeError:MailThread.message_post 需要 1 个位置参数,但是给出了 3 个。
下面是我的代码:
import json
import xmlrpc.client
db18 = 'test' # write your database
user18 = 'admin' # write your user
password18 = 'admin'
url18 = 'http://localhost:8018'
common18 = xmlrpc.client.ServerProxy('{}/xmlrpc/2/common'.format(url18))
uid18 = common18.authenticate(db18, user18, password18, {})
print("uid::::::::::::::::::::::", url18)
models18 = xmlrpc.client.ServerProxy('{}/xmlrpc/2/object'.format(url18),
allow_none=True)
print("models:::::::::::::::::::::::::", models18)
# Open the JSON file and load the data
with open('Tickets0.json', 'r') as file:
data = json.load(file)
for res in data:
# print(res)
description_html = res['helpdesk_ticket']['description_html']
# print("id::::::::", description_html)
helpdesk = models18.execute_kw(db18, uid18, password18, 'helpdesk.ticket',
'search', [[['helpdesk_id', '=', helpdesk_id]]])
print("helpdesk::::::::::::::::", helpdesk)
if helpdesk:
print("in if:::::::::::::::::::::", helpdesk[0])
log_note = "Ticket updated from Freshdesk with Subject"
try:
doc_id = models18.execute_kw(db18, uid18, password18, 'helpdesk.ticket',
'message_post', [[helpdesk[0]], description_html, {}])
print(f"Record updated with ID: {doc_id}")
except Exception as e:
print(f"Error creating record: {e}")
我不知道如何在调用方法时添加帮助台工单的记录 ID。我尝试使用以下方法提供它:-
doc_id = models18.execute_kw(db18, uid18, password18, 'helpdesk.ticket',
'message_post', [helpdesk[0]],[description_html])
但没有用,也搜索了一些帖子,但没有任何作用。
我是否做错了什么或者遗漏了什么?