JSON 在编译请求的文件或其依赖项之一期间发生错误。 ;预期的

JSON An error occurred during the compilation of the requested file, or one of its dependencies. ; expected

我正在尝试格式化此 JSON 以便能够将其正确分配给 DataProviderJSON。 它一直给我一个变量 'testing':

的错误

编译请求的文件或其依赖项之一期间发生错误。 ;预计

 using (ContentBySearchWebPart box = GetControlByIDPath(ControlID, this.Page.Controls[0]) as ContentBySearchWebPart)
    {
                    string testing;
                    testing = "{\"QueryGroupName\":\"Default\",\"QueryPropertiesTemplateUrl\":"",\"IgnoreQueryPropertiesTemplateUrl\":false,\"SourceID\":\"8413cd39-2156-4e00-b54d-11efd9abdb89\",\"SourceName\":\"Local SharePoint Results\",\"SourceLevel\":\"Ssa\",\"CollapseSpecification\":"",\"QueryTemplate\":\"#test (ContentTypeId:0x01FD4FB0210AB50249908EAA47E6BD3CFE8B* OR ContentTypeId:0x01FD59A0DF25F1E14AB882D2C87D4874CF84* OR ContentTypeId:0x012002* OR ContentTypeId:0x0107* OR WebTemplate=COMMUNITY)\",\"FallbackSort\":[],\"FallbackSortJson\":[],\"RankRules\":[],\"RankRulesJson\":\"[]\",\"AsynchronousResultRetrieval\":false,\"SendContentBeforeQuery\":true,\"BatchClientQuery\":true,\"FallbackLanguage\":-1,\"FallbackRankingModelID\":\"\",\"EnableStemming\":true,\"EnablePhonetic\":false,\"EnableNicknames\":false,\"EnableInterleaving\":false,\"EnableQueryRules\":true,\"EnableOrderingHitHighlightedProperty\":false,\"HitHighlightedMultivaluePropertyLimit\":-1,\"IgnoreContextualScope\":true,\"ScopeResultsToCurrentSite\":false,\"TrimDuplicates\":false,\"Properties\":{\"TryCache\":true,\"Scope\":\"{Site.URL}\",\"UpdateLinksForCatalogItems\":true,\"EnableStacking\":true,\"ListId\":\"8837b1d4-c29a-4ab0-9385-f483c19b25ec\",\"ListItemId\":4},\"PropertiesJson\":\"{\"TryCache\":true,\"Scope\":\"{Site.URL}\",\"UpdateLinksForCatalogItems\":true,\"EnableStacking\":true,\"ListId\":\"8837b1d4-c29a-4ab0-9385-f483c19b25ec\",\"ListItemId\":4}\",\"ClientType\":\"ContentSearchRegular\",\"UpdateAjaxNavigate\":true,\"SummaryLength\":180,\"DesiredSnippetLength\":90,\"PersonalizedQuery\":false,\"FallbackRefinementFilters\":null,\"IgnoreStaleServerQuery\":false,\"RenderTemplateId\":\"DefaultDataProvider\",\"AlternateErrorMessage\":null,\"Title\":""}";
                    box.DataProviderJSON = testing; }

更新:现在它似乎可以正常工作,但我在

上收到新错误

box.DataProviderJSON = testing;

抛出了 'System.Web.HttpUnhandledException' 类型的异常。 ---> System.ArgumentException: 传入的对象无效,应为“:”或“}”。

这是JSON:

"{\"QueryGroupName\":\"Default\",\"QueryPropertiesTemplateUrl\":\"\",\"IgnoreQueryPropertiesTemplateUrl\":false,\"SourceID\":\"8413cd39-2156-4e00-b54d-11efd9abdb89\",\"SourceName\":\"Local SharePoint Results\",\"SourceLevel\":\"Ssa\",\"CollapseSpecification\":\"\",\"QueryTemplate\":\"#test(ContentTypeId:0x01FD4FB0210AB50249908EAA47E6BD3CFE8B* OR ContentTypeId:0x01FD59A0DF25F1E14AB882D2C87D4874CF84* OR ContentTypeId:0x012002* OR ContentTypeId:0x0107* OR WebTemplate=COMMUNITY)  \",\"FallbackSort\":[],\"FallbackSortJson\":[],\"RankRules\":[],\"RankRulesJson\":\"[]\",\"AsynchronousResultRetrieval\":false,\"SendContentBeforeQuery\":true,\"BatchClientQuery\":true,\"FallbackLanguage\":-1,\"FallbackRankingModelID\":\"\",\"EnableStemming\":true,\"EnablePhonetic\":false,\"EnableNicknames\":false,\"EnableInterleaving\":false,\"EnableQueryRules\":true,\"EnableOrderingHitHighlightedProperty\":false,\"HitHighlightedMultivaluePropertyLimit\":-1,\"IgnoreContextualScope\":true,\"ScopeResultsToCurrentSite\":false,\"TrimDuplicates\":false,\"Properties\":{\"TryCache\":true,\"Scope\":\"Site.URL\",\"UpdateLinksForCatalogItems\":true,\"EnableStacking\":true,\"ListId\":\"8837b1d4-c29a-4ab0-9385-f483c19b25ec\",\"ListItemId\":4},\"PropertiesJson\":{\"TryCache\":true,\"Scope\":\"{Site.URL}\",\"UpdateLinksForCatalogItems\":true,\"EnableStacking\":true,\"ListId\":\"8837b1d4-c29a-4ab0-9385-f483c19b25ec\",\"ListItemId\":4},\"ClientType\":\"ContentSearchRegular\",\"UpdateAjaxNavigate\":true,\"SummaryLength\":180,\"DesiredSnippetLength\":90,\"PersonalizedQuery\":false,\"FallbackRefinementFilters\":null,\"IgnoreStaleServerQuery\":false,\"RenderTemplateId\":\"DefaultDataProvider\",\"AlternateErrorMessage\":null,\"Title\":\"\"}";

您没有在 QueryPropertiesTemplateUrl 之后转义 "。因此字符串在那里结束,编译器缺少 ; 来结束语句。

testing = "{\"QueryGroupName\":\"Default\",\"QueryPropertiesTemplateUrl\":" //; The string ends, but is not followed by an semicolon - thus the compile error. 

您更想使用

testing = "[...]\"QueryPropertiesTemplateUrl\":\"\",\"IgnoreQueryPropertiesTemplateUrl[...]";