jsonpath

Testing with spring-test-mvc jsonpath returns null

六眼飞鱼酱① 提交于 2019-12-19 04:04:19
问题 I am using Spring's "spring-test-mvc" library to test web controllers. I have a very simple controller that returns a JSON array. Then in my test I have: @Test public void shouldGetAllUsersAsJson() throws Exception { mockMvc.perform(get("/v1/users").accept(MediaType.APPLICATION_JSON)) .andExpect(content().mimeType(MediaType.APPLICATION_JSON)) .andExpect(jsonPath("fName").exists()); } The above test returns: java.lang.AssertionError: No value for JSON path: fName To quickly check what I

Json.NET JSONPath query not returning expected results

一曲冷凌霜 提交于 2019-12-17 20:14:33
问题 I'm using Newtonsoft's Json.Net to select nodes from the following json: { "projects":[ { "name":"Project 1", "client":{ "code":"ABC", "name":"Client 1" } }, { "name":"Project 2", "client":{ "code":"DEF", "name":"Client 2" } }, { "name":"Project 3", "client":{ "code":"GHI", "name":"Client 3" } } ] } The following c# snippet //json is a JObject representation of the json listed above var clients = json.SelectTokens("$.projects[*].client"); Yields: [ { "code":"ABC", "name":"Client 1" }, { "code

Newtonsoft JSON.Net SelectToken Issue

孤街醉人 提交于 2019-12-17 14:55:25
问题 I have the following query and the sample JSON. I try it on "http://jsonpath.com/" it works as expected. If I try it in VisualStudio it returns no results. $.Items.Services[?(@.Name == 'Another Service')].Url Here's the JSON: { "Items": { "Resource": { "Id": "12345" }, "Services": { "service1": { "Name": "My First Service", "Type": "WS", "Url": "https://server1/service1" }, "service2": { "Name": "Another Service", "Type": "WS", "Url": "https://server2/service2" } } } } And the sample code:

Python-Jsonpath简单入门

只愿长相守 提交于 2019-12-16 19:24:26
原文来自: http://goessner.net/articles/JsonPath/ JSONPath - 是xpath在json的应用。 xml最大的优点就有大量的工具可以分析,转换,和选择性的提取文档中的数据。XPath是这些最强大的工具之一。 如果可以使用xpath来解析json,以下的问题可以被解决: 1,数据不使用特殊的脚本,可以在客户端交互的发现并取并获取。 2,客户机请求的JSON数据可以减少到服务器上的相关部分,这样可以最大限度地减少服务器响应的带宽使用率。 如果我们愿意,这个可以解析json数据的工具会变得有意义。随之而来的问题是它如何工作,jsonpath的表达式看起来怎么样。 事实上,json是由c系统编程语言表示自然数据,有特定语言的特定语法来访问json数据。 xpath的表达式: /store/book[1]/title 我们可以看作是: x.store.book[0].title 或 x['store']['book'][0]['title'] 在Javascript, Python 和 PHP 中一个变量x表示json数据。经过观察,特定的语言里有内置xpath来解析数据。 JSONPath工具的问题 -依赖某种特定的语言 - 需要依赖XPath 1.0 - 减少代码量和内存的消耗 - 在运行时 JSONPath 表达式 JSONPath 是参照

JSONPath-简单入门

谁都会走 提交于 2019-12-16 17:25:05
JSONPath - 是xpath在json的应用。 xml最大的优点就有大量的工具可以分析,转换,和选择性的提取文档中的数据。XPath是这些最强大的工具之一。 如果可以使用xpath来解析json,以下的问题可以被解决: 1,数据不使用特殊的脚本,可以在客户端交互的发现并取并获取。 2,客户机请求的JSON数据可以减少到服务器上的相关部分,这样可以最大限度地减少服务器响应的带宽使用率。 如果我们愿意,这个可以解析json数据的工具会变得有意义。随之而来的问题是它如何工作,jsonpath的表达式看起来怎么样。 事实上,json是由c系统编程语言表示自然数据,有特定语言的特定语法来访问json数据。 xpath的表达式: /store/book[1]/title 我们可以看作是: x.store.book[0].title 或 x['store']['book'][0]['title'] 在Javascript, Python 和 PHP 中一个变量x表示json数据。经过观察,特定的语言里有内置xpath来解析数据。 JSONPath工具的问题 -依赖某种特定的语言 - 需要依赖XPath 1.0 - 减少代码量和内存的消耗 - 在运行时 JSONPath 表达式 JSONPath 是参照,xpath表达式来解析xml文档的方式

spring单元测试-MockMvc

喜你入骨 提交于 2019-12-16 09:18:25
MockMvc详解——SpringMVC单元测试 原文地址 一、简介 为何使用MockMvc? 对模块进行集成测试时,希望能够通过输入URL对Controller进行测试,如果通过启动服务器,建立http client进行测试,这样会使得测试变得很麻烦,比如,启动速度慢,测试验证不方便,依赖网络环境等,所以为了可以对Controller进行测试,我们引入了MockMVC。 MockMvc实现了对Http请求的模拟,能够直接使用网络的形式,转换到Controller的调用,这样可以使得测试速度快、不依赖网络环境,而且提供了一套验证的工具,这样可以使得请求的验证统一而且很方便。 二、测试逻辑 MockMvcBuilder构造MockMvc的构造器 mockMvc调用perform,执行一个RequestBuilder请求,调用controller的业务处理逻辑 perform返回ResultActions,返回操作结果,通过ResultActions,提供了统一的验证方式 使用StatusResultMatchers对请求结果进行验证 使用ContentResultMatchers对请求返回的内容进行验证 三、MockMvcBuilder MockMvc是spring测试下的一个非常好用的类,他们的初始化需要在setUp中进行。

Need a json path expression for below json

旧城冷巷雨未停 提交于 2019-12-14 04:04:10
问题 Need a JSON path expression for below JSON. I wanted to extract "Id" for each specific "name" For Example: I need to extract "Id" : "3" for "name" : "XYZ" . I tried a JSON path expression as $..Id which given output as: [ "1", "2", "3" ] But I needed an Id specific to "name": "XYZ"` [ { "primary":{ "name":"ABC" }, "Id":"1" }, { "primary":{ "name":"PQR" }, "Id":"2" }, { "primary":{ "name":"XYZ" }, "Id":"3" } ] 回答1: Able to resolve this by below expression $..[?(@.primary.name == 'XYZ')].Id 来源:

JsonPath with Newtonsoft.JSON

北慕城南 提交于 2019-12-14 03:47:48
问题 I tried for nearly an hour different approaches, but I don't get it ;( my JSON object is this: "typeOfHair": { "value": [ { "code": "Dry Hair", "values": [ { "value": "DryHair", "language": "en" }, { "value": "TrockenesHaar", "language": "de" } ] }, { "code": "Any Type of Hair", "values": [ { "value": "AnyTypeOfHair", "language": "en" }, { "value": "JedenHaartyp", "language": "de" } ] } ] } And my task is to get with Newtonsoft.JSON all values where the language is "de". My current approach

JsonPath OR condition using MockMVC

久未见 提交于 2019-12-14 03:03:17
问题 I am practicing MockMVC for rest call unit testing. how can we test the Boolean values so that whether the result is true or false I need to pass the test, I tried as follows, mockMvc.perform(get("/student/{Id}", 1L)). .andExpect(status().isOk()) .andExpect(jsonPath("$.isPass", is(true || false))); Also I have list with 6 values, how can use list contains all kind of method, .andExpect(jsonPath("$.subjectList", hasSize(5))) .andExpect(jsonPath("$.subjectList.name", Matchers.contains("English"

JSONPath expression to look inside different keys

我的未来我决定 提交于 2019-12-13 19:18:49
问题 I have a JSON returned by a RESTful API: { "0": { "id": "1484763", "name": "Name", "values": { "0": { "value": "Peter" } } }, "1": { "id": "2584763", "name": "phone", "values": { "0": { "value": "45456456" } } } } How do I write a JSONPath that extracts a phone number value? (so in this case, "45456456"). What makes the problem harder, phone number object is not always inside "1" key. 回答1: Try this JsonPath which results in phone number 45456456 $..[?(@.name = 'phone')].values.0.value