假设我有以下 DTO:
class C {
String a;
String b;
}
Run Code Online (Sandbox Code Playgroud)
我有 JSON:
{
"c" : {
"a" : "aaa",
"b" : "bbb"
}
}
Run Code Online (Sandbox Code Playgroud)
我想要做的是,完成以下测试:
C expected = new C("aaa","bbb");
mockMvc.perform(get("url"))
.andExpect(jsonPath("$.c", is(expected)));
Run Code Online (Sandbox Code Playgroud)
它失败。如果我首先序列化为expectedJSON 然后尝试匹配,它会再次失败,因为它是一个字符串。这可能吗?
问题:如果包含的功能,我想获得boolean价值trueArrayjsontest1
例子:
{
"features": [
"test1",
"test2",
"test3"
],
"name": "David"
}
Run Code Online (Sandbox Code Playgroud)
对于上面的 json,结果应该是true.
我有一个 Json 格式作为模板(Temp.json)。以下是我的模板格式
{
"products":[
{
"ProductTitleName": "",
"ImageUrl":""
}
]
}
Run Code Online (Sandbox Code Playgroud)
现在我必须将数据填充为这种格式,基本上产品数组将有许多对象节点。我已经使用 JsonPath 表达式从原始 Json 中提取相关属性值。我的问题是如何使用此模板并将数据填充到此结构中。
Reason to use Template Json ——
所以为了避免那些提到的原因,我计划有一个模板 json 并坚持该结构,这是一个好方法,如果是这样,请帮助我将数据填充到模板中,如果没有,请帮助我使用更好的方法。
我尝试了近一个小时的不同方法,但我不明白;(
我的 JSON 对象是这样的:
"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"
}
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
我的任务是使用 Newtonsoft.JSON 获取语言为“de”的所有值。我目前的做法是:
JsonObject.SelectTokens("typeOfHair.value.values[?(@.language == 'de')].value").ToList()
Run Code Online (Sandbox Code Playgroud)
有人可以帮我弄这个吗?
亲切的问候
我正在使用 MockMVC 测试一个 todo 控制器:
mockMvc.perform(MockMvcRequestBuilders.get("/toDos/")
.with(user("user").password("password").roles("ADMIN"))
.content("{ \"saved_date\": \"2010-01-01\"}")
.accept(MediaType.APPLICATION_JSON_VALUE))
.andExpect((ResultMatcher) jsonPath("$.id").doesNotExist())
.andExpect(status().isOk())
.andExpect( content().contentType("application/json"));
}
Run Code Online (Sandbox Code Playgroud)
我不断收到此错误:
java.lang.ClassCastException: org.springframework.test.web.client.match.JsonPathRequestMatchers$5 cannot be cast to org.springframework.test.web.servlet.ResultMatcher
Run Code Online (Sandbox Code Playgroud)
我想删除对 (ResultMatcher) 的转换,但不知道如何创建一个 ResultMatcher 来测试 Id 的存在。有任何想法吗 ?
我正在使用 Newtonsoft 的 Json.Net 从以下 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"
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
以下 c# 片段
//json is a JObject representation of the json listed above
var clients = json.SelectTokens("$.projects[*].client");
Run Code Online (Sandbox Code Playgroud)
产量:
[
{
"code":"ABC",
"name":"Client 1"
},
{
"code":"DEF",
"name":"Client 2"
},
{
"code":"GHI",
"name":"Client 3"
}
]
Run Code Online (Sandbox Code Playgroud)
这很酷,现在,我想做的是按客户端代码过滤,我认为
$.projects[*].client[?(@.code == 'DEF')]
Run Code Online (Sandbox Code Playgroud)
会工作,但我显然不太了解语法。这将返回一个空列表:
var test1 …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Python 从 JSON 中提取有用信息的不同方法。我尝试了 jsonpath_rw_ext 和 jsonpath_ng。现在我可以使用 jsonpath_rw_ext 但 jsonpath_ng 不起作用。我不想放弃 jsonpath_ng 因为我可能没有以正确的方式使用它。检查以下代码:
import jsonpath_rw_ext
from jsonpath_ng.ext import parse
import json
from pprint import pprint
json_str = '''{
"students": [
{"name": "Peter", "gender": "Male", "age": 20},
{"name": "Mary", "gender": "Female", "age": 30},
{"name": "Susan", "gender": "Female", "age": 40}
],
"teachers": [
{"name": "William", "gender": "Male", "age": 30},
{"name": "John", "gender": "Male", "age": 40},
{"name": "Lucy", "gender": "Female", "age": 50}
]
}'''
json_obj = json.loads(json_str)
print 'jsonpath_rw_ext:'
female_students = jsonpath_rw_ext.match('$.students[?gender=="Female"]', …Run Code Online (Sandbox Code Playgroud) 我正在尝试为我从下面的 json 中检索的 jtoken 设置一个值
{
"Customer":
{
"Id": "100",
"Country": "BE",
"CustomerName": "Domo",
"CustomerStatus": "Gold",
"CreationDate": {
"Time":""
}
}
}
Run Code Online (Sandbox Code Playgroud)
我正在设法使用以下 jPath 检索时间令牌:
客户.创建日期.时间
但是在尝试设置属性时获取空值:
var property = timeJToken as JProperty;
property.Value = value;
Run Code Online (Sandbox Code Playgroud) 我正在使用 JSON 并面临一些问题。
我想在 JSON 对象中插入/更新路径。在路径不存在的情况下,它将被创建,然后我插入一个新值。如果它退出,它将被一个新值更新
例如,我想添加这样的新路径:
val doc = JsonPath.parse(jsonString)
doc.add("$.user.name", "John")
Run Code Online (Sandbox Code Playgroud)
但我总是收到此错误,因为路径不存在:
com.jayway.jsonpath.PathNotFoundException 类:路径 $['user'] 中缺少属性
因此,如果它不存在,我想创建一个新路径。
这是我的代码,但jsonString不会改变:
var jsonString = "{}" val conf = Configuration.defaultConfiguration().addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL).addOptions(Option.SUPPRESS_EXCEPTIONS)
JsonPath.using(conf).parse(jsonString).set(JsonPath.compile("$.user.name"), "John")
Log.d("TAG", "new json = $jsonString")
Run Code Online (Sandbox Code Playgroud)
请给我你的建议。非常感谢!!
有人可以告诉我为什么这不起作用:
@Test
public void should_parse_json() {
Expression expression = new SpelExpressionParser().parseExpression("#jsonPath(get('JsonData'), '$.someData')");
Map<String, Object> data = new HashMap<>();
data.put("JsonData", "{\"someData\": 100}");
StandardEvaluationContext context = new StandardEvaluationContext(data);
context.addPropertyAccessor(new JsonPropertyAccessor());
assertThat(expression.getValue(context, Object.class)).isEqualTo(100);
}
Run Code Online (Sandbox Code Playgroud)
我收到错误“org.springframework.expression.spel.SpelEvaluationException:EL1006E:找不到函数‘jsonPath’”
我在类路径中有以下 jar:
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
SPEL 文档对我没有帮助。