如何将数据/键的逗号和文本的逗号区别格式化为json



我正在努力改进这段代码,以便它能够处理特定的情况。

目前它可以工作,除非用户添加带有逗号的文本

这是我的输入谁工作(只看"注意"键/值(

Input_OK =  2020-11-25,note:my text,2020-11-25,today:2020-11-25,2020-09-14,start:2020-09-14

在这种情况下:my text可以,因为没有逗号

Input_NOK =  2020-11-25,note:my text, doesn't work,2020-11-25,today:2020-11-25,2020-09-14,start:2020-09-14

在这种情况下:my text, doesn't work不正常,因为存在逗号

有了这个特定的输入2020-11-25,note:my text, work now,2020-11-25,today:2020-11-25,2020-09-14,start:2020-09-14我试着把这个输出

[{"release_date":"2020-11-25","today":"2020-11-25","note0":"my text, work now"},{"release_date":"2020-09-14","start":"2020-09-14"}]

这是我当前的代码

// before this input I add string to a list<String> for each date like that [2020-11-25,note0:test, 2020-11-24,my text, with comma, 2020-11-15,today:2020-11-15, 2020-09-14,start:2020-09-14] 
//After I remove space and [ ]
// myinput 2020-11-25,today:2020-11-25,2020-11-25,note0:my text, with comma,2020-09-14,start:2020-09-14
var inputItarable = myinput.toString().split(',').where((s) => s.isNotEmpty);
print("inputItarable   ${inputItarable} ");
//inputItarable   [2020-11-25, today:2020-11-25, 2020-11-25, note0:my text, with comma, 2020-09-14, start:2020-09-14] 

var i = inputItarable.iterator;

var tmp = {};
while (i.moveNext()) {
var key = i.current; i.moveNext();
var value = i.current.split(':');
(tmp[key] ??= []).add(value);
}

var output1 = tmp.keys.map((key) {
var map = {}; map['release_date'] = key;
tmp[key].forEach((e) => map[e[0]] = e[1]);
return map;
}).toList();
var output2=json.encode(output1);
print("output2   $output2 ");
// output2   [{"release_date":"2020-11-25","today":"2020-11-25","note0":"my text, with comma"},{"release_date":"2020-09-14","start":"2020-09-14"}]

[编辑]我有一个特殊的案例,用户反向对齐,并有一个类似的输入

myinput 2020-11-25,today:2020-11-25,2020-11-25,note0:my text, 
with comma,2020-09-14,start:2020-09-14

在这个例子中,我不知道如何用my text,nwith comma替换my text,with comma之间的反向对齐

请检查下面的代码,或者您可以直接在Dartpad上运行它https://dartpad.dev/1404509cc0b427b1f31705448b5edba3

我写了一个消毒函数。消毒功能的作用是对possibleStartpossibleEnd之间的文本进行消毒。这意味着它将用户输入文本中的所有逗号替换为§。为此,它假设用户输入以、注:,注0:开始,并以2020-2021-结束。这个经过净化的字符串被传递给你的代码,最后§被替换为"&";。如果你有任何问题,请告诉我。

import 'dart:convert';
String sanitize(
String input, List<String> possibleStart, List<String> possibleEnd) {
final String start = possibleStart.join("|");
final String end = possibleEnd.join("|");
final RegExp exp = RegExp("(?<=$start)(.*?)(?=$end)");
final Iterable<Match> matches = exp.allMatches(input);
matches.forEach((match) {
input =
input.replaceFirst(match.group(0), match.group(0).replaceAll(",", "§"));
return true;
});
return input;
}
void main() {
String myinput =
"2020-11-25,today:2020-11-25,2020-11-25,note0:my text, with comma,2020-09-14,start:2020-09-14";
myinput = sanitize(myinput, [",note:", "note\d:"], [",20\d\d-"]);
var inputItarable = myinput.toString().split(',').where((s) => s.isNotEmpty);
print("inputItarable   ${inputItarable} ");
//inputItarable   [2020-11-25, today:2020-11-25, 2020-11-25, note0:my text, with comma, 2020-09-14, start:2020-09-14]
var i = inputItarable.iterator;
var tmp = {};
while (i.moveNext()) {
var key = i.current;
i.moveNext();
var value = i.current.split(':');
(tmp[key] ??= []).add(value);
}
var output1 = tmp.keys.map((key) {
var map = {};
map['release_date'] = key;
tmp[key].forEach((e) => map[e[0]] = e[1]);
return map;
}).toList();
var output2 = json.encode(output1).replaceAll("§", ",");
print("output2   $output2 ");
}

最新更新