2016-04-10 35 views
0

我正在用Maven項目構建Jersey服務器,並且除了當我嘗試從C#程序發送請求時,我的一切都正常工作我已經指出用戶將與其數組進行交互JSON顯示爲: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; 
    } 
} 

任何建議讚賞。如有需要,我會根據要求發佈更多信息。

回答

1

您將people列表序列化爲json,然後使用request.AddJsonBody將其添加到請求中。但是AddJsonBody會再次將參數序列化爲json,所以最終得到一個雙重序列化的值 - 在服務器端 - 不能被解析爲trashList[]。因此刪除客戶端序列化代碼,並簡單地寫入

request.AddJsonBody(people);