我想将字符串转换为json对象,但无法将其转换。因为我使用的是带有特殊符号的字符串。所以我必须从JSON文件读取查询。这是我正在使用的文件,名称为 functions.json 。
[
{
"function":"Promo1",
"query1": "{%24and:[{\"createdOn\":{%24lte:\"1712086400000\"}},{\"country\": \"India\"},{\"Type\": \"App\"}]}",
"query2": "{\"country\": \"India\"}"
}
]
此文件中有很多对象。我必须先读取文件,然后才能动态获取函数名称,我必须从该对象中获取适当的函数查询。因此,请考虑我要 Promo1 查询从那个对象。我可以获取这个query1字段值。之后,我将取消转义来自query1字符串的特殊符号,以便我将 $ 而不是%24 >。我正在使用此代码对字符串var q1 = unescape(str_arry[0].query1);
进行转义。
取消转义后, q1 保留了一个字符串值{$and:[{"createdOn":{$lte:"1712086400000"}},{"country": "India"},{"Type": "App"}]}
。我正在使用此字符串将其转换为Object
{$and:[{"createdOn":{$lte:"1712086400000"}},{"country": "India"},{"Type": "App"}]}
由于$符号,我无法使用 JSON.parse 进行解析。我收到此错误
SyntaxError:JSON中位置1处的意外令牌$
有人可以帮我解决这个问题吗?
答案 0 :(得分:1)
如果这仍然是字符串,并且键始终像$ and,$ lte或$ abc。 这可以解决:
var json_str = '{$and:[{"createdOn":{$lte:"1712086400000"}},{"country": "India"},{"Type": "App"}]}';
// put quotes to all keys which start from $
json_str = json_str.replace(/\{(\$[a-zA-Z]+)\:/g,'{"$1":');
var obj = JSON.parse( json_str );
console.log( obj );
答案 1 :(得分:1)
避免此问题的简单方法是首先不将数据存储为JSON。如果...ng-value="{{actn.Id}}" >
是应用程序的一部分,则可以将数据存储为JS,并具有所有各自的优点,包括宽松的语法要求:
...ng-value="actn.Id" >
如果functions
成为字符串至关重要(尽管
这里不是这种情况,因为应该将module.exports = [
{
function: "Promo1",
query1: {$and:[{"createdOn":{$lte:"1712086400000"}},{"country": "India"},{"Type": "App"}]}
}
];
转换回对象),它可以就地进行字符串化,因此对象保持可读性:
query1
将查询存储为JSON的正确方法是根据JSON规范对其进行字符串化。如果手动编写有效的JSON有问题,可以将其输出为:
query1
或者,可以使用任何第三方JSON格式化程序。
答案 2 :(得分:0)
JSON还需要将所有属性键都用引号括起来才能有效,因此您需要先将json字符串更改为{"$and":[{"createdOn":{"$lte":"1712086400000"}},{"country": "India"},{"Type": "App"}]}
,然后才能解析它。
也许猫鼬有办法做到这一点,否则您就被字符串操作所困扰。
var json_str = '{"$and":[{"createdOn":{"$lte":"1712086400000"}},{"country": "India"},{"Type": "App"}]}';
var obj = JSON.parse( json_str );
console.log( obj );
答案 3 :(得分:0)
请稍等测试,也许可以解决您的问题
{"$and":[{"createdOn":{"$lte":"1712086400000"}},{"country": "India"},{"Type": "App"}]}
答案 4 :(得分:0)
我们可以借助JSON.stringify()方法解决此问题。
let dataWith$ = { '$and': [ { 'createdOn': {'$lte':"1712086400000"} },
{ 'country': 'India' },
{ 'Type': 'App' } ]
}
let parsedData = JSON.parse( JSON.stringify( dataWith$)); // It won't give you error
console.log( typeof parsedData ) // object
console.log( parsedData.$and) // [ { createdOn:{'$lte':'1712086400000'} }, { country: 'India' }, { Type: 'App' } ]
答案 5 :(得分:0)
public Boolean SplitByList(string povDocument, string povOutputDocument, PageLabelList povPageList)
{
PdfReader lorReader = null;
Document lorDocument = null;
PdfCopy lorPdfCopyProvider = null;
PdfImportedPage lorImportedPage = null;
PdfPageLabels lorLbls = null;
try
{
// Intialize a new PdfReader instance with the contents of the source Pdf file:
lorReader = new PdfReader(povDocument);
lorLbls = new PdfPageLabels();
if (povPageList == null || povPageList.GetLength() == 0) return false;
Boolean lbvFirstPage = false;
for (int livCntr = 1; livCntr <= povPageList.GetLength(); livCntr++)
{
PageLabel lorLabel = povPageList.GetItemAt(livCntr);
if (lorLabel != null && lorLabel.lbvPublish)
{
// Do only the first time
if (!lbvFirstPage)
{
lbvFirstPage = true;
lorDocument = new Document(lorReader.GetPageSizeWithRotation(lorLabel.livPageNumber));
// Initialize an instance of the PdfCopyClass with the source document and an output file stream:
lorPdfCopyProvider = new PdfCopy(lorDocument, new System.IO.FileStream(povOutputDocument, System.IO.FileMode.Create));
lorDocument.Open();
}
// Extract the desired page number:
lorImportedPage = lorPdfCopyProvider.GetImportedPage(lorReader, lorLabel.livPageNumber);
lorLbls.AddPageLabel(livCntr, 1, lorLabel.lorPageLabel);
lorPdfCopyProvider.AddPage(lorImportedPage);
}
}
lorPdfCopyProvider.Close();
lorDocument.Close();
lorReader.Close();
return true;
}
catch (Exception ex)
{
lorDocument.Close();
lorReader.Close();
return false;
}
}
并没有任何要求,这是您在parse
之后得到的。
parse
答案 6 :(得分:0)
在JSON键中使用特殊符号的原因,必须用双引号或单引号引起来。如果您无法手动更改, 您可以使用JSON stringify方法进行转换。
var convertToJson = {$and:[{"createdOn":{$lte:"1712086400000"}},{"country": "India"},{"Type": "App"}]}
convertToJson = JSON.stringify(convertToJson)
console.log(convertToJson)
然后您可以照常解析数据
console.log(JSON.parse(convertToJson))
答案 7 :(得分:0)
如果我了解您,您想将您的对象转换为有效的JSO对象吗?
在这种情况下,您可以像在这里一样解析对象的字符串化版本:
var obj = {$and:[{"createdOn":{$lte:"1712086400000"}},{"country": "India"},{"Type": "App"}]};
var jsonObj = JSON.parse(JSON.stringify(obj));
console.log(jsonObj);
另一个可能是使用JSON.parse()的eval insetad:
var objStr = [{
"function": "Promo1",
"query1": "{%24and:[{\"createdOn\":{%24lte:\"1712086400000\"}},{\"country\": \"India\"},{\"Type\": \"App\"}]}",
"query2": "{\"country\": \"India\"}"
}];
var q1 = eval('(' + unescape(objStr[0].query1) + ')');
console.log(q1);