You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Graph API查询JSON解析及调用报错问题排查求助

问题分析与解决:Graph API查询的JSON解析和语法错误问题

问题1:JSON解析失败(Invalid JSON query)

原始查询语句:

[{ "type": "url", "value": ["https://graph.microsoft.com/v1.0/users?$count=true&$search="displayName:room"&$filter=endsWith(mail,'microsoft.com')&$orderBy=displayName&$select=id,displayName,mail"]}]

错误原因:JSON字符串内部嵌套了未转义的双引号。URL中的$search="displayName:room"使用了双引号,而整个URL本身被JSON的双引号包裹,导致JSON语法结构混乱,JToken.Parse无法正确解析。

问题2:Graph API语法错误(单引号不合法)

修改为单引号包裹后:

[{ "type": "url", "value": ["https://graph.microsoft.com/v1.0/users?$count=true&$search='displayName:room'&$filter=endsWith(mail,'microsoft.com')&$orderBy=displayName&$select=id,displayName,mail"]}]

错误原因:Graph API的$search参数语法要求使用双引号包裹搜索表达式,单引号不符合其语法规范,因此抛出字符无效的错误。

正确的写法

需要同时满足JSON语法规范和Graph API的查询语法,两种可行方案如下:

方案1:在JSON中转义双引号

在JSON字符串里用\"表示双引号,既符合JSON解析要求,又能传递Graph API需要的格式:

[{ "type": "url", "value": ["https://graph.microsoft.com/v1.0/users?$count=true&$search=\"displayName:room\"&$filter=endsWith(mail,'microsoft.com')&$orderBy=displayName&$select=id,displayName,mail"]}]

方案2:对URL中的双引号进行编码

将双引号"编码为URL转义字符%22,JSON解析时无需额外转义,Graph API也能正确识别:

[{ "type": "url", "value": ["https://graph.microsoft.com/v1.0/users?$count=true&$search=%22displayName:room%22&$filter=endsWith(mail,'microsoft.com')&$orderBy=displayName&$select=id,displayName,mail"]}]

额外注意:使用$count=true时,请求必须设置ConsistencyLevel: eventual请求头,否则Graph API也会返回错误。

内容的提问来源于stack exchange,提问作者user989988

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 01:10:29