6

Jsonを使用して、ファイルをRailsサーバーにアップロードするac#メカニズムを作成しています。

ファイル部分に到達する前に、サーバーに投稿しようとしているだけで、サーバーに到着するjson文字列に問題があるようです..

私は何を間違っているのでしょうか? 文字列をシリアル化する 2 つの異なる方法と、既にシリアル化された文字列をロードする方法を既に試しました...

どうやらサーバーに送信されている文字列の最初と最後の両方にある二重引用符と関係があるのか​​ 、リクエストからそれらを削除する方法があるのでしょうか(引用符を囲んでWizTools.orgのRestClientを使用せずに、すべてうまくいきます大丈夫...) :

MultiJson::DecodeError (757: unexpected token at '"{\"receipt\":{\"total\":100.0,\"tag_number\":\"xxxxx\",\"ispaperduplicate\":true},\"machine\":{\"serial_number\":\"111111\",\"safe_token\":\"1Y321a\"}}"')

私のC#コード:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RestSharp;
using System.Web.Script.Serialization;
using Newtonsoft.Json;

namespace RonRestClient
{


    class templateRequest
    {
        public Receipt receipt;
        public class Receipt
        {
            public float total;
            public String tag_number;
            public bool ispaperduplicate = true;
            public Receipt(float total, String tagnr)
            {
                this.total = total;
                this.tag_number = tagnr;
            }
        };
        public Machine machine;
        public class Machine
        {
            public String serial_number;
            public String safe_token;
            public Machine(String machinenr, String safe_token)
            {
                this.serial_number = machinenr;
                this.safe_token = safe_token;
            }
        };
    }


    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            string path = @"C:\file.pdf";
            string tagnr = "p94tt7w";
            string machinenr = "2803433";
            string safe_token = "123";
            float total = 100;

            templateRequest req = new templateRequest();
            req.receipt = new templateRequest.Receipt(total, tagnr);
            req.machine = new templateRequest.Machine(machinenr, safe_token);
            //string json_body = JsonConvert.SerializeObject(req);
            //string json_body = new JavaScriptSerializer().Serialize(req);


            string json_body = @"{""receipt"" : {""total"":"+total+@", ""tag_number"":"""+tagnr+@""",""ispaperduplicate"":true},""machine"":{""serial_number"": """+machinenr+@""", ""safe_token"": """+safe_token+@"""}}";

            var client = new RestClient("http://localhost:3000");

            var request = new RestRequest("/receipts",Method.POST);


            //set request Body
            request.AddHeader("Content-type", "application/json");
            request.AddHeader("Accept", "application/json");
            request.RequestFormat = DataFormat.Json;

            request.AddBody(json_body); 
            //request.AddParameter("text/json", json_body, ParameterType.RequestBody);

            // easily add HTTP Headers


            // add files to upload (works with compatible verbs)
            //request.AddFile("receipt/receipt_file",path);

            // execute the request

            IRestResponse response = client.Execute(request);
            var content = response.Content; // raw content as string
            if(response.ErrorMessage !="") content += response.ErrorMessage;
            response_box.Text = content;


        }
    }
}
4

1 に答える 1

7

問題は、RestRequest.AddBody(ソース コード) メソッドが実際には、コンテンツが正しい形式にシリアル化されていないことを前提としていることです。

つまり、.NET 文字列を実際に JSON オブジェクトとして渡したいと認識しているのではなく、JSON 文字列として .NET 文字列を渡そうとしていると考えているということです。

ただし、これは、オブジェクトを自分で JSON にシリアル化することで、実際にはあまりにも多くの作業を行っていることを意味します。

行を置換

request.AddBody(json_body);

と:

request.AddBody(req);

プロパティを使用して、JSON へのシリアル化を行う方法を制御できRestRequest.JsonSerializerます。

絶対に JSON 文字列を保持したい場合は、次のように書きたいと思うかもしれません。

request.AddParameter("application/json", json_body, ParameterType.RequestBody);

(実際にそれを行うコメント行があるようです-なぜコメントしたのですか?)

于 2013-01-03T15:10:10.347 に答える