首页 \ 问答 \ Jackson Json访问JsonNode属性名称(Jackson Json accesing JsonNode property name)

Jackson Json访问JsonNode属性名称(Jackson Json accesing JsonNode property name)

我有这样的架构:

{
  "type" : "object",
  "$schema" : "http://json-schema.org/draft-03/schema#",
  "id" : "urn:jsonschema:com:vlashel:dto:UserDto",
  "description" : "this is the top description",
  "title" : "this is the top title",
  "properties" : {
    "number" : {
      "type" : "integer"
      "required" : true
    },
    "password" : {
      "type" : "string"
      "required" : true

    }
}

我有以下代码通过删除“required”将此shcema draft 3转换为draft 4,我想收集其中包含'requred'的节点属性名称。 我怎么做? 我没有看到这方面的方法..

             JsonNode jsonNode = jsonNodeIterator.next();
            ObjectNode element;
            if (jsonNode instanceof ObjectNode) {
                element = (ObjectNode) jsonNode;
                element.remove("required");
               String propertyName = element.getPropertyName(); //I'm looking for this kind of method.

谢谢!


I have a schema like this:

{
  "type" : "object",
  "$schema" : "http://json-schema.org/draft-03/schema#",
  "id" : "urn:jsonschema:com:vlashel:dto:UserDto",
  "description" : "this is the top description",
  "title" : "this is the top title",
  "properties" : {
    "number" : {
      "type" : "integer"
      "required" : true
    },
    "password" : {
      "type" : "string"
      "required" : true

    }
}

I have the following code that converts this shcema draft 3 to draft 4 by removing "required", I want to collect nodes property names that have 'requred' in them. How do I do that? I don't see methods for this..

             JsonNode jsonNode = jsonNodeIterator.next();
            ObjectNode element;
            if (jsonNode instanceof ObjectNode) {
                element = (ObjectNode) jsonNode;
                element.remove("required");
               String propertyName = element.getPropertyName(); //I'm looking for this kind of method.

Thanks!


原文:https://stackoverflow.com/questions/26108156
更新时间:2022-12-10 15:12

最满意答案

您不必使用foreach迭代对象,因为'age'是$data->faceAttributes一个属性。

改用它

if (count($data->faceAttributes)) {    
    echo $data->faceAttributes->age;
    echo $data->faceAttributes->gender;
}

但是, $data是一个数组,您使用的$data[0]实际上是$data[0]

所以,如果数据数组中只有一个元素,你可以这样做

$data = $data[0] or $data = json_decode($json)[0]

或者,如果多于一个元素,则可以遍历$data


You don't have to iterate the object using foreach as the 'age' is a property of $data->faceAttributes itself.

Use this instead

if (count($data->faceAttributes)) {    
    echo $data->faceAttributes->age;
    echo $data->faceAttributes->gender;
}

However, $data is an array and which $data you are using is actually $data[0]

So, if there is only one element in data array you can do

$data = $data[0] or $data = json_decode($json)[0]

Or, you can iterate through $data in case of more then one element.

相关问答

更多
  • 这是你的最终代码吗? 如果发生连接错误消息,您不会输出任何内容。 更改 $json = json_encode($results); 至 echo json_encode($results); 如果它仍为空白,则可能是数据库数据问题。 Is this your final code? You are not outputting anything apart from the connection error message if it happens. Change $json = json_e ...
  • 您可以使用.each遍历对象: $.getJSON("controllers/Customer.controller.php", param, function(json){ // loop over each object in the array // 'i' is the index of the object within the array // 'val' (or this) is the actual object at that offset $.each( ...
  • 有些数据库对象是西班牙文的,最有可能的是你的结果数组$arreglo没有用$arreglo -8编码。 由于西班牙语使用特殊字符如'ñ',所以很可能您的默认数据库编码不是utf-8。 遇到这种字符时,函数json_encode失败并不显示任何内容。 因为这个函数只能用于utf-8的结果。 处理这个问题的一个方法是通过这个函数传递数组,以便将所有内容都转换为utf-8。 将以下函数添加到您的PHP。 function cast_data_types ($value) { if (is_array ...
  • 您不必使用foreach迭代对象,因为'age'是$data->faceAttributes一个属性。 改用它 if (count($data->faceAttributes)) { echo $data->faceAttributes->age; echo $data->faceAttributes->gender; } 但是, $data是一个数组,您使用的$data[0]实际上是$data[0] 所以,如果数据数组中只有一个元素,你可以这样做 $data = $data[0] ...
  • 请参阅: http : //php.net/manual/en/function.exec.php#116222 无法从exec'd命令获取输出以显示在$ output数组中? 相反,它是否反映在你的外壳上? 在命令末尾附加“2>&1”,例如: exec("xmllint --noout ~/desktop/test.xml 2>&1", $retArr, $retVal); 将使用预期输出填充数组$ retArr; 每个数组键一行。 See: http://php.net/manual/en/functi ...
  • 你只需要使用第二个参数设置为TRUE json_decode() 。 $str = '...'; $json = json_decode($str, TRUE); $artist = $json['response']['artists']; //$artist = json_decode($str, TRUE)['response']['artists']; as of PHP 5.4 print_r($artist); 输出: Array ( [0] => Array ...
  • 如果你想要它作为一个数组,你需要使用它 $array = json_decode($var, true); 否则,它将返回json作为对象而不是数组 这样做应该让你像这样访问它: $fl = $array[0]['faceRectangle']['left']; You need to use this if you want it as an array $array = json_decode($var, true); Otherwise it will return the json as a ...
  • 改变这一点 $town = $POST_['town']; > $query = "Select * From weather WHERE town = '$town'"; 至 $town = $_POST['town']; $query = "Select * From weather WHERE town = '".$town."'"; change this $town = $POST_['town']; > $query = "Select * From weather WHERE ...
  • $selected_products = json_decode($_POST['data'], true); echo $selected_products[0]['id']; echo '
    '; echo $_POST; echo '
    '; var_dump($_POST); 将数据添加到您的帖子 你现在得到null因为那里给出了stringify。 请改变这样的代码。它肯定会起作用。 var data=[{"id":"RECOLOURBALM","option":"Bordeaux"," ...
  • 您将希望避免将第二个艺术家添加到随后进行json编码的中间数组中。 $tracks = array(); foreach ($result['tracks'] as $trackdata) { $songId = $trackdata['songId']; $title = $trackdata['title']; $ISRC = $trackdata['ISRC']; $name1 = $trackdata['artists:name[1]' ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。