2011-11-17 69 views
2

我已將C#中的字符串&傳遞給JSON值,需要將其轉換爲DataTable。將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(); 
    } 
} 

這是我的web服務的一部分....

// 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# code

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; 
    } 

當我調用webservice這個方法轉換部分給出錯誤。它說Additional text found in JSON string after finishing deserializing object.

這是我的JSON結果在C#

{ 
"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#I want to do the Convert JSON as String to DataTable

回答

3

JSON字符串應該是象下面這樣:

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

「列表」當作你的表名和行處理的DataTable
大括號內數據 要驗證json字符串,您可以使用此網站:http://jsonlint.com/

+0

是的。謝謝alsp correct.I在虛擬客戶端網站嘗試了這些東西。我有同樣的錯誤。糾正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'}' – Piraba

+0

我也是這樣做的,同樣的錯誤。這是在C#中的正確方法。我總是在這裏得到錯誤'DataTable dataTable = JsonConvert.DeserializeObject (jsonContent); ' – Piraba

+0

格式您的代碼完全一樣:'「[{」SpecialDiscountFlag「:」0「,」TotalLineDiscount「:」0「,」ExecutiveCode「:」0「}]」' – Priyank