c# - 在 C# 中将 JSON 作为字符串转换为 DataTable

标签 c# android wcf json web-services

我已将 JSON 值作为字符串传递,并且 C# 中需要转换为 DataTable。

我已经在android部分完成了,(将json作为字符串)

  public void getUploadTableData(){
    DBAdapter dbAdapter = DBAdapter.getDBAdapterInstance(this);
    dbAdapter.openDataBase();
    try {
        if(uploadTable.size() > 0){
            for (Map.Entry<Integer, String> entry : uploadTable.entrySet()) {
                int key = entry.getKey();
                String value = entry.getValue();

                JSONObject invHeader = new JSONObject();

                if(value.equals("WMInvoiceHeader")){
                     String query = "SELECT BusinessUnit,ExecutiveCode,InvoiceNo,SalesCategory,RetailerCode," +
                                    " RetailerCodeSon,InvoiceDate,GrossValue,InvoiceValue,TotalLineDiscount," +
                                    " FROM WMInvoiceHeader " +
                                    " WHERE (CancelFlag IS NULL OR CancelFlag ='0')";
                     ArrayList<?> stringList = dbAdapter.selectRecordsFromDBList(query, null);
                     if(stringList.size() > 0){
                            for (int i = 0; i < stringList.size(); i++) {
                                ArrayList<?> arrayList = (ArrayList<?>) stringList.get(i);
                                ArrayList<?> list = arrayList;

                                invHeader.put("BusinessUnit",(String)list.get(0));
                                invHeader.put("ExecutiveCode",(String)list.get(1));
                                invHeader.put("InvoiceNo",(String)list.get(2));
                                invHeader.put("SalesCategory",(String)list.get(3));
                                invHeader.put("RetailerCode",(String)list.get(4));
                                invHeader.put("RetailerCodeSon",(String)list.get(5));
                                invHeader.put("InvoiceDate",(String)list.get(6));
                                invHeader.put("GrossValue",(String)list.get(7));
                                invHeader.put("InvoiceValue",(String)list.get(8));
                                invHeader.put("TotalLineDiscount",(String)list.get(9));


                            }
                            System.out.println("----invHeader---" + invHeader.toString());
                     }
                     soapPrimitiveData("WMInvoiceHeader", strBusinessUnit, strExecutive, invHeader.toString());
                }

            //  System.out.println("----invHeader---" + invHeader.toString());
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

这是我的网络服务部分......

// ksoap2 calling wcf
public SoapPrimitive soapPrimitiveData(String tablename,String strBusinessUnit, String strExecutive,String jsonString) throws IOException,XmlPullParserException {
    SoapPrimitive responsesData = null;
    SoapObject requestData = new SoapObject(NAMESPACE, METHOD_NAME); // set

    requestData.addProperty("strBusinessUnit", strBusinessUnit);
    requestData.addProperty("strExecutive", strExecutive);
    requestData.addProperty("strTableName", tablename);
    requestData.addProperty("jsonContent", jsonString);
    SoapSerializationEnvelope envelopes = new SoapSerializationEnvelope(SoapEnvelope.VER11); // put all required data into a soap//// envelope
    envelopes.dotNet = true;
    envelopes.setOutputSoapObject(requestData);
    AndroidHttpTransport httpTransport = new AndroidHttpTransport(APPURL);
    httpTransport.debug = true;


    try {
        httpTransport.call(SOAP_ACTION, envelopes);
        responsesData = (SoapPrimitive) envelopes.getResponse();


    } catch (SocketException ex) {
        Log.e("Error : " , "Error on soapPrimitiveData() " + ex.getMessage());
        ex.printStackTrace();
    } catch (Exception e) {
        Log.e("Error : " , "Error on soapPrimitiveData() " + e.getMessage());
       e.printStackTrace();
    }
    return responsesData;
}

这是我的C# 代码

    public bool convertJSONToDataSet(string strBusinessUnit, string strExecutiveCode, string strTableName, string jsonContent)
    {
        bool status =false;

        DataTable dataTable = JsonConvert.DeserializeObject<DataTable>(jsonContent);
        status  = UpdateUploadData(strBusinessUnit, strExecutiveCode, strTableName, dataTable);
        return status;
    }

当我调用Web服务时,此方法转换部分给出错误。它说完成反序列化对象后在JSON字符串中找到其他文本。

这是我在 C# 中的 json 结果

{
"SpecialDiscountFlag": "0",
"TotalLineDiscount": "0",
"ExecutiveCode": "TEST001",
"InvoiceValue": "3000",
"InvoiceDate": "2011-11-17",
"RouteCode": "VRT002",
"RetailerCode": "TEST0007",
"HeaderDiscountFlag": "1",
"GrossValue": "3000",
"UploadedOn": "2011-11-17",
"SalesType": "O",
"VisitNumber": "26",
"UploadFlag": "1",
"InvoiceNo": "26",
"SalesCategory": "VSAO",
"BusinessUnit": "MASS",
"VisitSequence": "1",
"UploadedBy": "TEST001",
"TotalHeaderDiscount": "0"
}

请告诉我这里出了什么问题。

我想在 C# 中将 JSON 作为字符串转换为 DataTable

最佳答案

Json 字符串应如下所示:

{
    "List": [
        {
            "ProjectId": 504,
            "RowId": 1,
            "ProjectName": "Google",
            "Member": "Private"
        },
        {
            "ProjectId": 503,
            "RowId": 2,
            "ProjectName": "Facebook",
            "Member": "Public"
        }
    ]
}

其中“List”被视为表名称,大括号内的数据被视为DataTable的行
要验证 json 字符串,您可以使用此站点:http://jsonlint.com/

关于c# - 在 C# 中将 JSON 作为字符串转换为 DataTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8166862/

相关文章:

c# - 实现自定义 Int+Range 列表解决方案

c# - 如果构造函数具有初始值设定项,那么构造函数在 C# 中需要一个主体是否有原因?

c# - 为什么在 RelayCommand 中使用弱引用?

c# - 获取 SortedList 中 2 个键之间所有键的最快方法是什么?

c# - 计算机电话集成软电话架构问题

c# - WCF 端口不监听

android - 如何创建像 Hangouts 应用程序一样的 MenuDrawer?

android - 使用 onListItemClick() 从 ListFragment 启动新 Activity

android - 在 Androidx 上使用 registerForActivityResult 选择文件

c# - 如何使用WF4检索MVC5中的页面流逻辑