我正在嘗試拆分檔案中具有兩個有效 JSON 字串的字串。我的資料看起來像這樣
{
"abc": {
"Version": "1.0",
"Allocations": [
{
"xyz": "40014645",
"efg": -5.0
}
],
"Date": "2022-10-11T07:29:00.000Z"
},
"tyu": 255,
"pou": 2139,
}
{
"abc": {
"Version": "1.0",
"Allocations": [
{
"xyz": "40014644",
"efg": -3.0,
}
],
"Date": "2022-10-11T08:50:00.000Z"
},
"tyu": 255,
"pou": 2139,
}
我的要求是將兩個 JSON 分開并在變數中獲取單獨的 JSON 字串,例如
{
"abc": {
"Version": "1.0",
"Allocations": [
{
"xyz": "40014645",
"efg": -5.0
}
],
"Date": "2022-10-11T07:29:00.000Z"
},
"tyu": 255,
"pou": 2139,
}
和
{
"abc": {
"Version": "1.0",
"Allocations": [
{
"xyz": "40014644",
"efg": -3.0,
}
],
"Date": "2022-10-11T08:50:00.000Z"
},
"tyu": 255,
"pou": 2139,
}
誰能幫我這個?架構本身沒有任何類。它只是一個帶有一些值的字串格式檔案。
uj5u.com熱心網友回復:
這可能有點古怪,但我將按原樣使用您的字串并假設您無法更改它。我將把它轉換成一個 json 陣列,為此我們需要用方括號括起來,但還要在每個外部大括號后添加逗號。這個代碼片段為我做了。
Stack<char> myStack = new Stack<char>();
string s = "";
//Find each outside curly brace and add a comma on the closing brace
foreach(char c in jsonString){
if(c == '{'){
myStack.Push(c);
}
if(c == '}'){
myStack.Pop();
if(myStack.Count == 0){
//Closing curly, add a comma after
s = c;
s = ',';
continue;
}
}
s = c;
}
//Wrap in brackets to turn into array
s = s.Insert(0, "[");
s = "]";
var options = new JsonSerializerOptions()
{
AllowTrailingCommas = true
};
var obj = JsonSerializer.Deserialize<object[]>(s, options);
Console.WriteLine(obj[0].ToString());
Console.WriteLine(obj[1].ToString());
uj5u.com熱心網友回復:
如果一個檔案中只有 2 個 json,我會嘗試這樣的
var i = 0;
var countStart = false;
for (i = 0; i < text.Length; i )
{
if (text[i] == '}') countStart = true;
else if (countStart)
{
if (text[i] == ',' || text[i] == ']') countStart = false;
else if (text[i] == '{') break;
}
}
var json1 = text.Substring(0, i - 1);
var json2 = text.Substring(i);
或者最好將其轉換為 json 陣列
text = "[" text.Substring(0, i - 1) ",\n" text.Substring(i) "]";
var jsonArray=JArray.Parse(text);
string version1 = jsonArray[0]["abc"]["Version"].ToString(); // "1.0"
如果你有超過 2 個 json,這個演算法也可以很容易地調整。
var countStart = false;
var indexList = new List<int> { 0 };
for (var i = 0; i < text.Length; i )
{
if (text[i] == '}') countStart = true;
else if (countStart)
{
if (text[i] == ',' || text[i] == ']') countStart = false;
else if (text[i] == '{') { indexList.Add(i); countStart = false; }
}
}
var json = "[";
for (var i = 0; i < indexList.Count; i )
{
if (i == indexList.Count - 1)
json = text.Substring(indexList[i]);
else
json = text.Substring(indexList[i], indexList[i 1] - indexList[i]) ",";
}
json = "]";
var jsonArray = JArray.Parse(json);
uj5u.com熱心網友回復:
string.Replace您可以同時使用和來實作您的目標string.Split。我復制了您在問題中發布的 json,并粘貼到名為json.txt
internal class Program
{
private static void Main(string[] args)
{
string text = File.ReadAllText("json.txt");
// inject curly brackets so that we can split without loosing
// one on each side
text = text.Replace("}\r\n{", "}}\r\n{{");
var jsonArr = text.Split("}\r\n{");
// print 1st json
Console.WriteLine("This the 1st json");
Console.WriteLine(jsonArr[0]);
// print 2nd json
Console.WriteLine("This the 2nd json");
Console.WriteLine(jsonArr[1]);
}
}
輸出:
This the 1st json
{
"abc": {
"Version": "1.0",
"Allocations": [
{
"xyz": "40014645",
"efg": -5.0
}
],
"Date": "2022-10-11T07:29:00.000Z"
},
"tyu": 255,
"pou": 2139,
}
This the 2nd json
{
"abc": {
"Version": "1.0",
"Allocations": [
{
"xyz": "40014644",
"efg": -3.0,
}
],
"Date": "2022-10-11T08:50:00.000Z"
},
"tyu": 255,
"pou": 2139,
}

轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/514887.html
標籤:C#json。网
