Я пытаюсь построить JSON из логического оператора (заданного в виде строки) на C #. Итак, я могу определить запрос и его подзапрос.
Ввод:
string statment = "(Entity.Country = 'USA' AND Entity.ShortName = 'Adele' AND (Entity.CIFNumber = '12345' OR Statement.StatementYear = '2015'))";
Мой класс, как показано ниже.
`class DataSetCommonQuery
{
public string @operator;
public List<JObject> rules = new List<JObject>();
}`
So far, I am able to accomplish the below JSON:
"operator": "AND",
"rules": [
{
"field": "ENTITY.ShortName ",
"condition": "=",
"value": "Adele"
},
{
"field": "ENTITY.Country",
"condition": "=",
"value": "USA"
}]
Пока я идентифицирую подзапрос из ввода:
(Entity.CIFNumber = '12345' OR Statement.StatementYear = '2015')
Ожидается JSON вроде:
"operator": "AND",
"rules": [
{
"field": "ENTITY.ShortName ",
"condition": "=",
"value": "Adele"
},
{
"field": "ENTITY.Country",
"condition": "=",
"value": "USA"
}
{
"operator": "AND",
rules[
{
"field": "ENTITY.CIFNumber",
"condition": "=",
"value": "12345"
},
{
"field": "Statement.StatementYear",
"condition": "=",
"value": "2015"
} ]
]
Но мой код возвращается:
"operator": "AND",
"rules": [
{
"field": "ENTITY.ShortName ",
"condition": "=",
"value": "Adele"
},
{
"field": "ENTITY.Country",
"condition": "=",
"value": "USA"
}
{
"field": "ENTITY.CIFNumber",
"condition": "=",
"value": "12345"
},
{
"field": "Statement.StatementYear",
"condition": "=",
"value": "2015"
}]
Это то, что я пробовал до сих пор:
public static DataSetCommonQuery ConvertToJsonObject (string BraconContents) {
string[] operators = splitWithOperator(bracketContents);
commonQuery.@operator = ReturnOperator(bracketContents);
string output;
do
{
//bracketContents = getWhatsInsideBrackets(bracketContents);
for (int i = 0; i < splitWithOperator(bracketContents).Length; i++)
{
var jObject = new JObject();
if(!checkIfBracketsExists(operators1[i]))
{
List<string> eachCondition = splitEachCondition(operators1[i].Trim());
eachCondition.Add(operators1[i].Replace(eachCondition[0], "").Replace(eachCondition[1], "").Trim());// Substring(operators1[i].IndexOf(eachCondition[0]), (operators1[i].IndexOf(eachCondition[1]) - operators1[i].IndexOf(eachCondition[0]))));
jObject.Add("field", eachCondition[0]);
jObject.Add("condition", eachCondition[2]);
jObject.Add("value", eachCondition[1]);
}
else if (checkIfBracketsExists(operators1[i]))
{
ConvertToJsonObject(getWhatsInsideBrackets(operators1[i]));
}
commonQuery.rules.Add(jObject);
//output = JsonConvert.SerializeObject(commonQuery);
}
} while (checkIfBracketsExists(bracketContents));
return commonQuery;
}
Есть предложения или идеи?