簡體   English   中英

如何在Java中使用JSON發送源代碼?

[英]How to send a source code using JSON in java?

我正在嘗試使用JSON對象從Servlet發送C / C ++代碼

        //server end
        response.setContentType("application/json");
        JSONObject jsonCode = new JSONObject();

        json.put("code", code); //this have a C/C++ code

        json.put("success", true);

        //client end

        function getCode(pointer)
        {

            var id = pointer.data.id;
            var json = { "id" : id };
                     $.ajax({
                "url" : "getCode",
                        "data" : json,
                                 "type" : "POST",

                                          "success" : function(data)
                {

                    alert(data.success); //undefined why ?
                    console.log(data);
                    if (!data.success) {

                        alert('code cannot be fetched');
                    }
                }
                ,
                    "error" : function(xhr, status, error)
                {
                    console.log('error');
                    console.log(xhr.responseText); //this is giving me the JSON ??

                    editor.setValue(xhr.responseText);
                }

但是錯誤函數不是成功的函數,而是錯誤函數? 請幫幫我

編輯:這是示例json,這是我嘗試發送的

    enter code here
    {"code":#include<iostream>

    class bbb{

    bbb(int array[12][12]=NULL){


    }

    };

    class aaa{

    };
    int func(int=12,int=123);
    int func(int x,int y){
    std::cout<<x<<y<<std::endl;
    }


    int main()
    {
    int array[12];
    int x=12;

    func(13);

    if(1==x){

    }
    //add your code here
    return 0;
    }
    ,"success":true}

現在我也使用RestMan對其進行了測試,當包含代碼時,它看起來好像JSON損壞了。有人可以幫助我嗎?

包含代碼的JSON文件是無效的JSON。

在有效的JSON文件中,字符串值必須用雙引號(“ value”)括起來,並且換行符必須使用\\ n進行轉義。 請勿使用JSON,而應單獨發送代碼,或使用轉義保留字符的JSON編碼器。

語法上有效的JSON文件應如下所示:

{
    "code":"#include<iostream>\n\nclass bbb{\n\nbbb(int array[12][12]=NULL){\n\n\n}...",
    "success":true
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM