简体   繁体   English

我如何在java中实现以下代码

[英]how can i acieve the following code in java

this code is in php where there are different type of json objects in an array此代码在 php 中,其中数组中有不同类型的 json 对象

function data($text, $number, $user_id, $port = NULL, $encoding = NULL)
    {
    $data = array
    (
        "text" => "#param#",
        "param" => array
        (
            array(
            "number" => $number,
            "text_param" => array($text),
            "user_id" => $user_id,
            ),
        ),
    );
    if ($port != NULL) {
        $data["port"] = array($port);
    }

    if ($encoding != NULL) {
        $data["encoding"] = $encoding;
    }
    return $data;
}

what are the possible ways to write the above code in java在java中编写上述代码的可能方法是什么

Don't look for direct, native support of translating json straight into Java objects, it needs to be done through libraries.不要寻找直接、原生的支持将 json 直接翻译成 Java 对象,它需要通过库来完成。

There are a (quite big) number of json libraries - GSON is one that I used, java EE json support is another.有(相当多)数量的 json 库 - GSON是我使用的一个, java EE json 支持是另一个。 Search for others using your preferred search engine.使用您首选的搜索引擎搜索其他人。

使用Jackson库将 JSON 解析为 Java 对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM