JSON序列化时为jsonrpc4j null

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JSON序列化时为jsonrpc4j null相关的知识,希望对你有一定的参考价值。

我从类jsonrpc4j中的CleengAPI.java中得到null错误,从中调用方法rpc.listCustomers(publisherToken, offset, limit);以序列化以下客户JSON列表,该错误由jsonrpc4j正确下载并显示)在debug语句中,但是JSON没有有效地序列化,从而导致空错误。

客户列表JSON


    "result": [
        
            "email": "",
            "firstName": "xx",
            "lastName": "xx",
            "dateOfBirth": "",
            "country": "US",
            "companyName": "",
            "phoneNumber": "",
            "addressLine1": null,
            "addressLine2": null,
            "city": null,
            "state": null,
            "postalCode": null,
            "regDate": "2019-12-24 07:06:46",
            "lastLoginDate": "2019-12-24 07:06:46",
            "transactions": "",
            "payment": "",
            "termsAccepted": "no",
            "marketingOptIn": "no",
            "lastUserIp": "xx.xx.x.x"
        ,
        
            "email": "11@gmail.com",
            "firstName": "xx",
            "lastName": "xx",
            "dateOfBirth": "",
            "country": "US",
            "companyName": "",
            "phoneNumber": "",
            "addressLine1": null,
            "addressLine2": null,
            "city": null,
            "state": null,
            "postalCode": null,
            "regDate": "2019-02-12 02:10:57",
            "lastLoginDate": "2019-03-19 17:10:28",
            "transactions": "1",
            "payment": "visa",
            "termsAccepted": "no",
            "marketingOptIn": "yes",
            "lastUserIp": "xxx.xxx.xx.x"
        
    ],
    "id": "854190206",
    "error": null,
    "jsonrpc": "2.0"

Class CleengAPI

import java.net.MalformedURLException;
import java.net.URL;

import com.googlecode.jsonrpc4j.JsonRpcHttpClient;
import com.googlecode.jsonrpc4j.JsonRpcParam;
import com.googlecode.jsonrpc4j.ProxyUtil;
import com.jahtoe.getner.domain.AccessStatus;
import com.jahtoe.getner.domain.Items;
import com.jahtoe.getner.domain.ListCustomers;
import com.jahtoe.getner.domain.ListCustomerSubscription;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CleengApi 

    private CleengRpc rpc;
    public static final Logger LOG = LoggerFactory.getLogger(CleengApi.class);

    private interface CleengRpc 

        Items<ListCustomers> listCustomers(
                @JsonRpcParam(value = "publisherToken") String publisherToken,
                @JsonRpcParam(value = "offset") int offset,
                @JsonRpcParam(value = "limit") int limit);

    

    public CleengApi() throws MalformedURLException 
        makeRpc("https://api.cleeng.com/3.0/json-rpc");
    

    private void makeRpc(String apiUrl) throws MalformedURLException 
        JsonRpcHttpClient client = new JsonRpcHttpClient(new URL(apiUrl));
        rpc = ProxyUtil.createClientProxy(getClass().getClassLoader(), CleengRpc.class, client);
    

    public Items<ListCustomers> ListCustomer(String publisherToken, int offset, int limit) 
        return rpc.listCustomers(publisherToken, offset, limit);
        


Class ListCustomers要序列化JSON

import java.io.Serializable;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.map.annotate.JsonSerialize;
@JsonIgnoreProperties(ignoreUnknown = true)

@JsonSerialize(include=JsonSerialize.Inclusion.NON_EMPTY)
public class ListCustomers implements Serializable 

    private static final long serialVersionUID = 1L;
    public String email;
    public String firstName;
    public String lastName;
    public String dateOfBirth;
    public String country;
    public String companyName;
    public String phoneNumber;
    public Object addressLine1;
    public Object addressLine2;
    public Object city;
    public Object state;
    public Object postalCode;
    public String regDate;
    public String lastLoginDate;
    public String transactions;
    public String payment;
    public String termsAccepted;
    public String marketingOptIn;
    public String lastUserIp;

创建CleanAPI并执行ListCustomers下载和序列化的代码段

CleengApi api = new 
allListCustomer = api.ListCustomer(publisherToken, 0, 50);

POM.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.jahtoe.XX</groupId>
    <artifactId>XX</artifactId>
    <version>1</version>
    <packaging>jar</packaging>

    <name>GregGetner</name>
    <description>ETL: Cleeng to Zapier</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.github.briandilley.jsonrpc4j</groupId>
            <artifactId>jsonrpc4j</artifactId>
            <version>1.5.3</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.13</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                </configuration>
            </plugin>
        </plugins>
    </build>    
</project>

Items Class

package com.cleeng.apiv3.domain;

import java.io.Serializable;

public class Items<X> implements Serializable 

    private static final long serialVersionUID = 1L;

    public int totalItemCount;
    public java.util.Vector<X> items;

我不确定如何调试此消息,因为消息只是一个单词null

任何帮助将不胜感激。

关于Conteh

答案

我看不到https://api.cleeng.com/3.0/json-rpc中的方法列表客户

您确定不反对使用此方法吗?

另一答案

所以发生的是,API返回的以下JSON样式需要Items类


    "result": 
        "items": [
            "offerId": "S321834685_US",
            "status": "active",
            "expiresAt": 15869957,
            "nextPaymentPrice": 79,
            "nextPaymentCurrency": "USD",
            "paymentGateway": "adyen",
            "paymentMethod": "visa"
        ],
        "totalItemCount": 1
    ,
    "id": "299789347",
    "error": null,
    "jsonrpc": "2.0"

但是此样式需要一个列表。我正在使用具有这种样式的Items导致null。


    "result": [
        
            "email": "",
            "firstName": "vvv",
            "lastName": "wwwww",
            "dateOfBirth": "",
            "country": "US",
            "companyName": "",
            "phoneNumber": "",
            "addressLine1": null,
            "addressLine2": null,
            "city": null,
            "state": null,
            "postalCode": null,
            "regDate": "2019-12-24 07:06:46",
            "lastLoginDate": "2019-12-24 07:06:46",
            "transactions": "",
            "payment": "",
            "termsAccepted": "no",
            "marketingOptIn": "no",
            "lastUserIp": "xx.xxx.xxx.xxx"
        ,

以上是关于JSON序列化时为jsonrpc4j null的主要内容,如果未能解决你的问题,请参考以下文章

JSON 到 c# 类序列化返回 null

json序列化NULL

JSON序列化自己主动过滤NULL值

关于序列化成JSON是NULL值转成空字符串,要怎么搞

JSON 序列化从本地文件返回 null

使用 JSON.NET 反序列化自定义 null 处理