【问题标题】:JSON Array Won't Map In JerseyJSON 数组不会映射到泽西岛
【发布时间】:2016-04-10 03:06:19
【问题描述】:

我正在使用 Maven 项目构建 Jersey 服务器,并且一切正常,除了当我尝试从 C# 程序发送请求时,我发现用户将与 JSON 显示为的数组进行交互:

[{"Weight":0.0,"RFIDCode":0},{"Weight":55.5,"RFIDCode":1}]

当我将代码发送到服务器时,我最终得到一个空的 Java 数组 我的 C# 代码如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.IO;
using RestSharp;

namespace POSTJSONList
{
    class Program
    {
        static void Main(string[] args)
        {
            List<ExampleMen> people = new List<ExampleMen>();
            JsonSerializer ser = new JsonSerializer();
            for (int i = 0; i < 5; i++)
            {
                ExampleMen person1 = new ExampleMen(i, i * 55.5);
                people.Add(person1);
            }
            string json = JsonConvert.SerializeObject(people);
            Console.WriteLine(json);
            Console.ReadLine();
            var client = new RestClient("http://localhost:9998");
            var request = new RestRequest("/trash", Method.POST);
            request.AddJsonBody(json);
            client.ExecuteAsync(request, response => {
                Console.WriteLine(response.Content);
            });
            Console.ReadLine();
        }
    }
}

我的 Java 代码如下

@POST
    @Produces("application/json")
    @Consumes(MediaType.APPLICATION_JSON)
    //@Param recycleList a list of all of the data that we have collected
    public String createtrash_records(trashpickup_type trashList[]){
        System.out.println("Im Here Guys!!!!");
        // TODO update this to take the information we pass through to it and create individual objects based on it. 
        // TODO modify to return success code
        billing billSystem = billing.getInstance();
        systemTotalsTrash sysTotalsTrashSystem = systemTotalsTrash.getInstance();
        int i = 1;
        for(trashpickup_type alpha : trashList)
        {   
            System.out.println(alpha.getRFIDCode() + alpha.getWeight());
            i++;
            /*try {
                billSystem.updateBillingRecord(rec.getUserName(), 0, rec.getWeight());
                sysTotalsTrashSystem.updateSystemTotals(rec.getWeight());
                getDao().createIfNotExists(rec);

            } catch (SQLException e) {
                e.printStackTrace();
            }
            */
        }
        return "It worked";
    }

模型代码(JSON 映射到的代码)在这里

package com.gallup.gethip.model;

import java.util.Date;

import javax.xml.bind.annotation.XmlRootElement;

import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;

@XmlRootElement
@DatabaseTable(tableName="trash_records")
public class trashpickup_type {
    @DatabaseField(columnName = "RFIDCode")
    private int RFIDCode;
    @DatabaseField(columnName = "Weight")
    private double Weight;
    @DatabaseField(columnName = "PickedUp")
    private Date PickedUp;
    public trashpickup_type()
    {

    }
    public void setRFIDCode(int RFIDCode)
    {
        this.RFIDCode = RFIDCode;
    }
    public void setWeight(double Weight)
    {
        this.Weight = Weight;
    }
    public void setPickedUP(Date PickedUp)
    {
        this.PickedUp = PickedUp;
    }

    public double getWeight()
    {
        return Weight;
    }
    public double getRFIDCode()
    {
        return RFIDCode;
    }
    public Date getPickedUp()
    {
        return PickedUp;
    }
}

感谢任何建议。如果需要,我会根据要求发布更多信息。

【问题讨论】:

    标签: java c# json jersey


    【解决方案1】:

    您将people 列表序列化为json,然后使用request.AddJsonBody 将其添加到请求中。但是AddJsonBody 将再次将参数序列化为 json,因此您最终会得到一个双序列化值,在服务器端无法将其解析为trashList[]。所以去掉客户端序列化代码,直接写

    request.AddJsonBody(people);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-06
      • 1970-01-01
      • 2011-10-14
      • 2015-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-02
      相关资源
      最近更新 更多