首页 \ 问答 \ 如何使用Jackson /其他库将json响应对象映射到首选格式?(How to map json response object to a preferred format using Jackson / other library?)

如何使用Jackson /其他库将json响应对象映射到首选格式?(How to map json response object to a preferred format using Jackson / other library?)

我从第三方Web服务获得以下JSON响应格式:

{
    "meta": {
        "code": 200,
        "requestId": "1"
    },
    "response": {
        "locations": [
            {
                "id": "1",
                "name": "XXX",
                "contact": {
                    phone: '123',
                    email: 'abc'
                },
                "location": {
                    "address": [
                        "Finland"
                    ]
                }
            },
            {
                // another location
            }
        ]
    }
}

以下是我应该从我自己的Web服务返回的内容:

[
    {
        "id": "1",
        "name": "XXX",
        "phone": '123',
        "address": "Finland"
    },
    {
        // another location
    }
]

我该怎么办? 我已经阅读了一些关于杰克逊的好东西,但是只有几个简单的例子,你将一些简单的JSON obj映射到POJO。 在我的例子中,我需要删除一些节点,并在层次结构中更深入地遍历以获取嵌套值。 到目前为止,这是我的春季启动应用中的宝贝步骤:

    @GET
    @Path("{query}")
    @Produces("application/json")
    public String getVenues(@PathParam("query") String query){
        return client.target(url).queryParam("query",query).request(...).get(String.class)
    }

任何帮助,指针,建议都欢迎!


I am getting the below JSON response format from a third party web service:

{
    "meta": {
        "code": 200,
        "requestId": "1"
    },
    "response": {
        "locations": [
            {
                "id": "1",
                "name": "XXX",
                "contact": {
                    phone: '123',
                    email: 'abc'
                },
                "location": {
                    "address": [
                        "Finland"
                    ]
                }
            },
            {
                // another location
            }
        ]
    }
}

And here is what I should return as a response from my own web service:

[
    {
        "id": "1",
        "name": "XXX",
        "phone": '123',
        "address": "Finland"
    },
    {
        // another location
    }
]

What should I do? I've read some good stuff about Jackson but there are only a few simple examples where you map some simple JSON obj as is to POJO. In my case, I need to remove a few nodes, and also traverse deeper down the hierarchy to get the nested value. This is my baby step so far in my spring boot app:

    @GET
    @Path("{query}")
    @Produces("application/json")
    public String getVenues(@PathParam("query") String query){
        return client.target(url).queryParam("query",query).request(...).get(String.class)
    }

Any helps, pointers, recommendations are welcomed!


原文:https://stackoverflow.com/questions/35191510
更新时间:2023-10-09 22:10

最满意答案

我把你的代码弹出一个编辑器来试一试。

我想你想要根据你的需要做一些改变。

   nav ul li ul {
     display: none;
   }

   nav ul li:hover > ul {
     display:block;
     position:absolute;
     top: 100%;
     left: 0;
   }

   nav ul li {
     position: relative;
   }

这将使下拉显示在正确的位置。 他们仍然需要造型但position:absolute与相对容器阻止他们影响周围的布局,我相信你想要的。

希望能帮到你!

注意::您可以看到更改如何影响以下代码段。

nav ul li ul {
display: none;}

nav ul li:hover > ul {
display:block;
position:absolute;
top: 100%;
left: 0;
}

nav ul li {
  position: relative;
}
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link href="../CSS/Main.css" rel="stylesheet" type="text/css">
</head>

<body background="../backgroundImage/UtahMoutains-copy7.png"style="background-repeat:no-repeat; background-position:center; background-attachment:fixed; background-size:cover; margin:0px;">

    <div style=" float:left; width:100%; background-color:#DDD margin:0; position:relative; margin:0px;">

        <img src="../backgroundImage/utahGovLogo.png" style=" float:left; padding:20px;">

        <img src="../Images/Search Button.png" style="float:right; padding:20px;">

    </div>
    <nav>
        <ul style="list-style:none; text-align:center; background-color:#DDD; font-family: Arial,Helvetica, Neue,sans-serif; font-size:120%; color:#666; margin:0px;">
            <li style="display: inline-block; margin-top:20px; margin-left:2%; margin-right:2%; margin-bottom:10px;"><a href="#" style=""> Government </a>
                <ul style="text-decoration:none; list-style:none;">
                    <li><a href="#" style="">State Agencies</a></li>
                    <li><a href="#" style="">Governors Office</a></li>
                    <li><a href="#" style="">Executive Branch</a></li>
                    <li><a href="#" style="">Legislative Branch</a></li>
                    <li><a href="#" style="">Judicial Branch</a></li>
                    <li><a href="#" style="">City & County Government</a></li>
                    <li><a href="#" style="">Federal Government</a></li>
                    <li><a href="#" style="">Election & Voting</a></li>
                    <li><a href="#" style="">Utah Code & Constitution</a></li>
                    <li><a href="#" style="">State Telephone Directory</a></li>
                    <li><a href="#" style="">Utah Data</a></li>
                    <li><a href="#" style="">Open.Utah.gov</a></li>
                </ul><!-- end of  Government  drop down menu-->
            </li><!-- end of Government Button-->
            <li style="display: inline-block; margin-top:20px; margin-left:2%; margin-right:2%; margin-bottom:10px;"><a href="#"> Services </a>
              <ul style="text-decoration:none; list-style:none;">
                    <li><a href="#" style="">Utah.gov Online Services</a></li>
                    <li><a href="#" style="">Citizen Services</a></li>
                    <li><a href="#" style="">Business Services</a></li>
                    <li><a href="#" style="">Gov to Gov Services</a></li>
                    <li><a href="#" style="">Financial</a></li>
                    <li><a href="#" style="">Legal</a></li>
                    <li><a href="#" style="">Utah.gov Registration</a></li>
                    <li><a href="#" style="">Citizen Alerts</a></li>
                    <li><a href="#" style="">Trending</a></li>
                </ul>

            </li>
            <li style="display: inline-block; margin-top:20px; margin-left:2%; margin-right:2%; margin-bottom:10px;"><a href="#"> Employment </a>
              <ul style="text-decoration:none; list-style:none;">
                    <li><a href="#" style="">Find a Job</a></li>
                    <li><a href="#" style="">Education & Training</a></li>
                    <li><a href="#" style="">State Employment</a></li>
                    <li><a href="#" style="">Careers</a></li>
                </ul>
            </li>
            <li style="display: inline-block; margin-top:20px; margin-left:2%; margin-right:2%; margin-bottom:10px;"><a href="#"> Business </a>
              <ul style="text-decoration:none; list-style:none;">
                    <li><a href="#" style="">Online Business Serices</a></li>
                    <li><a href="#" style="">Starting a Business</a></li>
                    <li><a href="#" style="">Running a Business</a></li>
                    <li><a href="#" style="">Relocating a Business</a></li>
                    <li><a href="#" style="">Dissolving a Business</a></li>
                    <li><a href="#" style="">Assess your Business Idea</a></li>
                    <li><a href="#" style="">Select your Business Structure</a></li>
                    <li><a href="#" style="">Prepare a Written Business Plan</a></li>
                    <li><a href="#" style="">Government Business Requirments</a></li>
                    <li><a href="#" style="">Financial Resources</a></li>
                </ul>
            </li>
            <li style="display: inline-block; margin-top:20px; margin-left:2%; margin-right:2%; margin-bottom:10px;"><a href="#"> Education </a>
              <ul style="text-decoration:none; list-style:none;">
                    <li><a href="#" style="">K - 12 Education</a></li>
                    <li><a href="#" style="">Colleges & Universities</a></li>
                    <li><a href="#" style="">Educational Resources</a></li>
                    <li><a href="#" style="">STEM</a></li>
                    <li><a href="#" style="">Utah History for Kids</a></li>
                    <li><a href="#" style="">Homework Help</a></li>
                    <li><a href="#" style="">Libraries</a></li>
                    <li><a href="#" style="">Student Network</a></li>
                    <li><a href="#" style="">School Reports</a></li>
                </ul>
            </li>
            <li style="display: inline-block; margin-top:20px; margin-left:2%; margin-right:2%; margin-bottom:10px;"><a href="#"> Resident </a>
              <ul style="text-decoration:none; list-style:none;">
                    <li><a href="#" style="">Citizens & Community</a></li>
                    <li><a href="#" style="">Health & Social Serives</a></li>
                    <li><a href="#" style="">Newcomers Guide</a></li>
                    <li><a href="#" style="">Moving to Utah</a></li>
                    <li><a href="#" style="">Cars & Transportation</a></li>
                    <li><a href="#" style="">Utah Laws</a></li>
                    <li><a href="#" style="">Tax Information</a></li>
                    <li><a href="#" style="">RSS ( Just for Youth)</a></li>
                    <li><a href="#" style="">Seniors.Utah.gov</a></li>
                    <li><a href="#" style="">Help your Neighbor</a></li>
                </ul>
            </li>
            <li style="display: inline-block; margin-top:20px; margin-left:2%; margin-right:2%; margin-bottom:10px;"><a href="#"> Visiting </a>
              <ul style="text-decoration:none; list-style:none;">
                    <li><a href="#" style="">Travel & Tourism</a></li>
                    <li><a href="#" style="">State Parks</a></li>
                    <li><a href="#" style="">Arts & Culture</a></li>
                    <li><a href="#" style="">Highway Information</a></li>
                    <li><a href="#" style="">Sports & Recreation</a></li>
                    <li><a href="#" style="">Arts & Leisure</a></li>
                </ul>
            </li>
            <li style="display: inline-block; margin-top:20px; margin-left:2%; margin-right:2%; margin-bottom:10px;"><a href="#"> About </a>
            </li>
            <li style="display: inline-block; margin-top:20px; margin-left:2%; margin-right:2%; margin-bottom:10px;"><a href="#"> Connect </a>
              <ul style="text-decoration:none; list-style:none;">
                    <li><a href="#" style="">Facebook</a></li>
                    <li><a href="#" style="">Twitter</a></li>
                    <li><a href="#" style="">Google +</a></li>
                    <li><a href="#" style="">Photos</a></li>
                    <li><a href="#" style="">YouTube</a></li>
                    <li><a href="#" style="">Mobile</a></li>
                </ul>
            </li>
        </ul>
    </nav>


I popped your code into an editor to give it a go.

I think you're looking to make slight changes for what you need.

   nav ul li ul {
     display: none;
   }

   nav ul li:hover > ul {
     display:block;
     position:absolute;
     top: 100%;
     left: 0;
   }

   nav ul li {
     position: relative;
   }

This will make the drop downs appear in the right position. They will still need styling but position:absolute with a relative container stops them from affecting the surrounding layout which is I believe what you desire.

Hope that helps you out!

NOTE:: You can see how the changes affect with the following snippet.

nav ul li ul {
display: none;}

nav ul li:hover > ul {
display:block;
position:absolute;
top: 100%;
left: 0;
}

nav ul li {
  position: relative;
}
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link href="../CSS/Main.css" rel="stylesheet" type="text/css">
</head>

<body background="../backgroundImage/UtahMoutains-copy7.png"style="background-repeat:no-repeat; background-position:center; background-attachment:fixed; background-size:cover; margin:0px;">

    <div style=" float:left; width:100%; background-color:#DDD margin:0; position:relative; margin:0px;">

        <img src="../backgroundImage/utahGovLogo.png" style=" float:left; padding:20px;">

        <img src="../Images/Search Button.png" style="float:right; padding:20px;">

    </div>
    <nav>
        <ul style="list-style:none; text-align:center; background-color:#DDD; font-family: Arial,Helvetica, Neue,sans-serif; font-size:120%; color:#666; margin:0px;">
            <li style="display: inline-block; margin-top:20px; margin-left:2%; margin-right:2%; margin-bottom:10px;"><a href="#" style=""> Government </a>
                <ul style="text-decoration:none; list-style:none;">
                    <li><a href="#" style="">State Agencies</a></li>
                    <li><a href="#" style="">Governors Office</a></li>
                    <li><a href="#" style="">Executive Branch</a></li>
                    <li><a href="#" style="">Legislative Branch</a></li>
                    <li><a href="#" style="">Judicial Branch</a></li>
                    <li><a href="#" style="">City & County Government</a></li>
                    <li><a href="#" style="">Federal Government</a></li>
                    <li><a href="#" style="">Election & Voting</a></li>
                    <li><a href="#" style="">Utah Code & Constitution</a></li>
                    <li><a href="#" style="">State Telephone Directory</a></li>
                    <li><a href="#" style="">Utah Data</a></li>
                    <li><a href="#" style="">Open.Utah.gov</a></li>
                </ul><!-- end of  Government  drop down menu-->
            </li><!-- end of Government Button-->
            <li style="display: inline-block; margin-top:20px; margin-left:2%; margin-right:2%; margin-bottom:10px;"><a href="#"> Services </a>
              <ul style="text-decoration:none; list-style:none;">
                    <li><a href="#" style="">Utah.gov Online Services</a></li>
                    <li><a href="#" style="">Citizen Services</a></li>
                    <li><a href="#" style="">Business Services</a></li>
                    <li><a href="#" style="">Gov to Gov Services</a></li>
                    <li><a href="#" style="">Financial</a></li>
                    <li><a href="#" style="">Legal</a></li>
                    <li><a href="#" style="">Utah.gov Registration</a></li>
                    <li><a href="#" style="">Citizen Alerts</a></li>
                    <li><a href="#" style="">Trending</a></li>
                </ul>

            </li>
            <li style="display: inline-block; margin-top:20px; margin-left:2%; margin-right:2%; margin-bottom:10px;"><a href="#"> Employment </a>
              <ul style="text-decoration:none; list-style:none;">
                    <li><a href="#" style="">Find a Job</a></li>
                    <li><a href="#" style="">Education & Training</a></li>
                    <li><a href="#" style="">State Employment</a></li>
                    <li><a href="#" style="">Careers</a></li>
                </ul>
            </li>
            <li style="display: inline-block; margin-top:20px; margin-left:2%; margin-right:2%; margin-bottom:10px;"><a href="#"> Business </a>
              <ul style="text-decoration:none; list-style:none;">
                    <li><a href="#" style="">Online Business Serices</a></li>
                    <li><a href="#" style="">Starting a Business</a></li>
                    <li><a href="#" style="">Running a Business</a></li>
                    <li><a href="#" style="">Relocating a Business</a></li>
                    <li><a href="#" style="">Dissolving a Business</a></li>
                    <li><a href="#" style="">Assess your Business Idea</a></li>
                    <li><a href="#" style="">Select your Business Structure</a></li>
                    <li><a href="#" style="">Prepare a Written Business Plan</a></li>
                    <li><a href="#" style="">Government Business Requirments</a></li>
                    <li><a href="#" style="">Financial Resources</a></li>
                </ul>
            </li>
            <li style="display: inline-block; margin-top:20px; margin-left:2%; margin-right:2%; margin-bottom:10px;"><a href="#"> Education </a>
              <ul style="text-decoration:none; list-style:none;">
                    <li><a href="#" style="">K - 12 Education</a></li>
                    <li><a href="#" style="">Colleges & Universities</a></li>
                    <li><a href="#" style="">Educational Resources</a></li>
                    <li><a href="#" style="">STEM</a></li>
                    <li><a href="#" style="">Utah History for Kids</a></li>
                    <li><a href="#" style="">Homework Help</a></li>
                    <li><a href="#" style="">Libraries</a></li>
                    <li><a href="#" style="">Student Network</a></li>
                    <li><a href="#" style="">School Reports</a></li>
                </ul>
            </li>
            <li style="display: inline-block; margin-top:20px; margin-left:2%; margin-right:2%; margin-bottom:10px;"><a href="#"> Resident </a>
              <ul style="text-decoration:none; list-style:none;">
                    <li><a href="#" style="">Citizens & Community</a></li>
                    <li><a href="#" style="">Health & Social Serives</a></li>
                    <li><a href="#" style="">Newcomers Guide</a></li>
                    <li><a href="#" style="">Moving to Utah</a></li>
                    <li><a href="#" style="">Cars & Transportation</a></li>
                    <li><a href="#" style="">Utah Laws</a></li>
                    <li><a href="#" style="">Tax Information</a></li>
                    <li><a href="#" style="">RSS ( Just for Youth)</a></li>
                    <li><a href="#" style="">Seniors.Utah.gov</a></li>
                    <li><a href="#" style="">Help your Neighbor</a></li>
                </ul>
            </li>
            <li style="display: inline-block; margin-top:20px; margin-left:2%; margin-right:2%; margin-bottom:10px;"><a href="#"> Visiting </a>
              <ul style="text-decoration:none; list-style:none;">
                    <li><a href="#" style="">Travel & Tourism</a></li>
                    <li><a href="#" style="">State Parks</a></li>
                    <li><a href="#" style="">Arts & Culture</a></li>
                    <li><a href="#" style="">Highway Information</a></li>
                    <li><a href="#" style="">Sports & Recreation</a></li>
                    <li><a href="#" style="">Arts & Leisure</a></li>
                </ul>
            </li>
            <li style="display: inline-block; margin-top:20px; margin-left:2%; margin-right:2%; margin-bottom:10px;"><a href="#"> About </a>
            </li>
            <li style="display: inline-block; margin-top:20px; margin-left:2%; margin-right:2%; margin-bottom:10px;"><a href="#"> Connect </a>
              <ul style="text-decoration:none; list-style:none;">
                    <li><a href="#" style="">Facebook</a></li>
                    <li><a href="#" style="">Twitter</a></li>
                    <li><a href="#" style="">Google +</a></li>
                    <li><a href="#" style="">Photos</a></li>
                    <li><a href="#" style="">YouTube</a></li>
                    <li><a href="#" style="">Mobile</a></li>
                </ul>
            </li>
        </ul>
    </nav>

相关问答

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)