-4
    type=CREATE
resource=transaction
number_of_attempts=1
data={"id":"11e7df5131dd9820bf774044","payment_method":"cc","account_vault_id":null,"recurring_id":null,"first_six":"411111","last_four":"1111","account_holder_name":" MALCOLM BLACK","transaction_amount":"1.00","description":null,"transaction_code":null,"avs":null,"batch":"2","order_num":"798279608211","verbiage":"APPROVAL","transaction_settlement_status":null,"effective_date":null,"routing":null,"return_date":null,"created_ts":1513092547,"modified_ts":1513092547,"transaction_api_id":null,"terms_agree":null,"notification_email_address":null,"notification_email_sent":true,"response_message":null,"auth_amount":"1.00","auth_code":"df5132","status_id":101,"type_id":20,"location_id":"11e7c499daaff1f0a7507909","reason_code_id":1000,"contact_id":"11e7c619b69d209ab41b24dc","billing_zip":"","billing_street":null,"product_transaction_id":"11e7c499db893b0489bb3739","tax":"0.000","customer_ip":null,"customer_id":null,"po_number":null,"avs_enhanced":"V","cvv_response":"N","billing_phone":null,"billing_city":null,"billing_state":null,"clerk_number":null,"tip_amount":"0.00","created_user_id":"11e7c499dac79026a4e740f1","modified_user_id":"11e7c499dac79026a4e740f1","settle_date":null,"charge_back_date":null,"void_date":null,"account_type":"visa","is_recurring":false,"is_accountvault":false,"transaction_c1":null,"transaction_c2":null,"transaction_c3":null,"additional_amounts":[],"terminal_serial_number":null,"entry_mode_id":"K","terminal_id":null,"quick_invoice_id":null,"emv_receipt_data":null}

This is the Form data i am receiving in C# using

string data=Request.Form("data");

Now data is nested how can i get the values of data object. Parsing it in json using JsonConvert.Deserialize is throwing exception.

The original response is:

type=CREATE&resource=transaction&number_of_attempts=1&data=%7b%22id%22%3a%2211e7df5131dd9820bf774044%22%2c%22payment_method%22%3a%22cc%22%2c%22account_vault_id%22%3anull%2c%22recurring_id%22%3anull%2c%22first_six%22%3a%22411111%22%2c%22last_four%22%3a%221111%22%2c%22account_holder_name%22%3a%22+MALCOLM+BLACK%22%2c%22transaction_amount%22%3a%221.00%22%2c%22description%22%3anull%2c%22transaction_code%22%3anull%2c%22avs%22%3anull%2c%22batch%22%3a%222%22%2c%22order_num%22%3a%22798279608211%22%2c%22verbiage%22%3a%22APPROVAL%22%2c%22transaction_settlement_status%22%3anull%2c%22effective_date%22%3anull%2c%22routing%22%3anull%2c%22return_date%22%3anull%2c%22created_ts%22%3a1513092547%2c%22modified_ts%22%3a1513092547%2c%22transaction_api_id%22%3anull%2c%22terms_agree%22%3anull%2c%22notification_email_address%22%3anull%2c%22notification_email_sent%22%3atrue%2c%22response_message%22%3anull%2c%22auth_amount%22%3a%221.00%22%2c%22auth_code%22%3a%22df5132%22%2c%22status_id%22%3a101%2c%22type_id%22%3a20%2c%22location_id%22%3a%2211e7c499daaff1f0a7507909%22%2c%22reason_code_id%22%3a1000%2c%22contact_id%22%3a%2211e7c619b69d209ab41b24dc%22%2c%22billing_zip%22%3a%22%22%2c%22billing_street%22%3anull%2c%22product_transaction_id%22%3a%2211e7c499db893b0489bb3739%22%2c%22tax%22%3a%220.000%22%2c%22customer_ip%22%3anull%2c%22customer_id%22%3anull%2c%22po_number%22%3anull%2c%22avs_enhanced%22%3a%22V%22%2c%22cvv_response%22%3a%22N%22%2c%22billing_phone%22%3anull%2c%22billing_city%22%3anull%2c%22billing_state%22%3anull%2c%22clerk_number%22%3anull%2c%22tip_amount%22%3a%220.00%22%2c%22created_user_id%22%3a%2211e7c499dac79026a4e740f1%22%2c%22modified_user_id%22%3a%2211e7c499dac79026a4e740f1%22%2c%22settle_date%22%3anull%2c%22charge_back_date%22%3anull%2c%22void_date%22%3anull%2c%22account_type%22%3a%22visa%22%2c%22is_recurring%22%3afalse%2c%22is_accountvault%22%3afalse%2c%22transaction_c1%22%3anull%2c%22transaction_c2%22%3anull%2c%22transaction_c3%22%3anull%2c%22additional_amounts%22%3a%5b%5d%2c%22terminal_serial_number%22%3anull%2c%22entry_mode_id%22%3a%22K%22%2c%22terminal_id%22%3anull%2c%22quick_invoice_id%22%3anull%2c%22emv_receipt_data%22%3anull%7d

UPDATE:

try
                {

                    string data = @Request.Form.Get("data");


    //now data=
 //{"id":"11e7df5131dd9820bf774044","payment_method":"cc","account_vault_id":n//ull.....}
                    //data = '"' + data + '"';
                    var stringfy = JsonConvert.DeserializeObject(data);

                    File.WriteAllText(path, " File Data : " + Request.Form +
                    Environment.NewLine + "     --------------------Total Value 0 " + Request.Form.Keys[0].ToString()
                    + Environment.NewLine + "     --------------------Total Value 1 " + Request.Form.Keys[1].ToString()
                    + Environment.NewLine + "     --------------------Total Value 2 " + Request.Form.Keys[2].ToString()
                     + Environment.NewLine + "     --------------------Total Value 3 " + data);
                }
                catch (Exception ex)
                {
                    File.WriteAllText(path, "Exception :1 "+ex.Message.ToString());
                }

Code is deployed on server so writing exceptions to file but, no code after Deserialize statement is working and no file is being created.

ccaring
  • 53
  • 7
  • 1
    So your question isn't about parsing form data, it's about parsing a JSON string. Read [ask] and at least show the exception, as well as your research for it. – CodeCaster Dec 12 '17 at 15:48
  • 1
    Deserializing the JSON to an object is certainly how I'd do it. What did you try and how did it fail? Actually examining the code you wrote and the error(s) you received would be a *great* first step in correcting the problem. – David Dec 12 '17 at 15:49
  • Did you try using `JsonSerializerSettings` with a `MissingMemberHandling.Ignore` to validate any unexpected fields are "tossed" as opposed to triggering an exception? – gravity Dec 12 '17 at 15:50
  • This is not json, its form data, using above Request.Form("data") it returns string so i was trying to deserialize it using json but its not working. – ccaring Dec 12 '17 at 15:50
  • @ccaring what do you think the form data item "data" contains? JSON perhaps? –  Dec 12 '17 at 15:51
  • 1
    @ccaring You have to provide us the exception detail, or we can't isolate what may be happening. – gravity Dec 12 '17 at 15:51
  • its a 3rd party response and my code is deployed on server, I cant get the exception, else i would have solved it myself. I just need to know how to parse this Formdata having nested object /member – ccaring Dec 12 '17 at 15:53
  • Possible duplicate of [Easiest way to parse JSON response](https://stackoverflow.com/questions/34043384/easiest-way-to-parse-json-response) (or if not a duplicate, then at least it should be very helpful) – Peter B Dec 12 '17 at 15:54
  • it is not JSON let me post real one – ccaring Dec 12 '17 at 15:55
  • @ccaring: What specifically is in your `data` variable? Is it the `data` value in your sample, or is it that entire URL-encoded string that you just added to the question? – David Dec 12 '17 at 15:58
  • @David its entire URL-encoded string – ccaring Dec 12 '17 at 15:59
  • @ccaring: Ah, *that's* what's been missing in the back-and-forth so far. I'm not aware of any specific tools for parsing that format, but if you can perform some string manipulation to extract only the value associated with `data=` (regular expressions, substrings, `string.Split()`, whatever works) then *that value*, once URL-decoded, *is* valid JSON and can then be deserialized. – David Dec 12 '17 at 16:02
  • yes i was doing so and i got the string , in question the top code shows the string of data value which i thought can be deserialized usign JSONCOnverter but i am unable to track whats the error. – ccaring Dec 12 '17 at 16:05
  • @ccaring: Well, the entirety of that first string isn't JSON. Just the part after `data=`. You'd want to strip out everything from the string except the `{...}` part. – David Dec 12 '17 at 16:09
  • @David please view above edit, it may help more – ccaring Dec 12 '17 at 16:10

1 Answers1

0

There is a shortcut. You can use this website as a helper - http://json2csharp.com/

All you need to do is past this string {"id":"11e7d...} then click generate.

// To parse this JSON data, add NuGet 'Newtonsoft.Json' then do:
//
//    using QuickType;
//
//    var data = Welcome.FromJson(jsonString);
//

namespace QuickType
{
    using System;
    using System.Net;
    using System.Collections.Generic;
    using Newtonsoft.Json;

public partial class Welcome
{
    [JsonProperty("account_holder_name")]
    public string AccountHolderName { get; set; }

    [JsonProperty("account_type")]
    public string AccountType { get; set; }

    [JsonProperty("account_vault_id")]
    public object AccountVaultId { get; set; }

    [JsonProperty("additional_amounts")]
    public object[] AdditionalAmounts { get; set; }

    [JsonProperty("auth_amount")]
    public string AuthAmount { get; set; }

    [JsonProperty("auth_code")]
    public string AuthCode { get; set; }

    [JsonProperty("avs")]
    public object Avs { get; set; }

    [JsonProperty("avs_enhanced")]
    public string AvsEnhanced { get; set; }

    [JsonProperty("batch")]
    public string Batch { get; set; }

    [JsonProperty("billing_city")]
    public object BillingCity { get; set; }

    [JsonProperty("billing_phone")]
    public object BillingPhone { get; set; }

    [JsonProperty("billing_state")]
    public object BillingState { get; set; }

    [JsonProperty("billing_street")]
    public object BillingStreet { get; set; }

    [JsonProperty("billing_zip")]
    public string BillingZip { get; set; }

    [JsonProperty("charge_back_date")]
    public object ChargeBackDate { get; set; }

    [JsonProperty("clerk_number")]
    public object ClerkNumber { get; set; }

    [JsonProperty("contact_id")]
    public string ContactId { get; set; }

    [JsonProperty("created_ts")]
    public long CreatedTs { get; set; }

    [JsonProperty("created_user_id")]
    public string CreatedUserId { get; set; }

    [JsonProperty("customer_id")]
    public object CustomerId { get; set; }

    [JsonProperty("customer_ip")]
    public object CustomerIp { get; set; }

    [JsonProperty("cvv_response")]
    public string CvvResponse { get; set; }

    [JsonProperty("description")]
    public object Description { get; set; }

    [JsonProperty("effective_date")]
    public object EffectiveDate { get; set; }

    [JsonProperty("emv_receipt_data")]
    public object EmvReceiptData { get; set; }

    [JsonProperty("entry_mode_id")]
    public string EntryModeId { get; set; }

    [JsonProperty("first_six")]
    public string FirstSix { get; set; }

    [JsonProperty("id")]
    public string Id { get; set; }

    [JsonProperty("is_accountvault")]
    public bool IsAccountvault { get; set; }

    [JsonProperty("is_recurring")]
    public bool IsRecurring { get; set; }

    [JsonProperty("last_four")]
    public string LastFour { get; set; }

    [JsonProperty("location_id")]
    public string LocationId { get; set; }

    [JsonProperty("modified_ts")]
    public long ModifiedTs { get; set; }

    [JsonProperty("modified_user_id")]
    public string ModifiedUserId { get; set; }

    [JsonProperty("notification_email_address")]
    public object NotificationEmailAddress { get; set; }

    [JsonProperty("notification_email_sent")]
    public bool NotificationEmailSent { get; set; }

    [JsonProperty("order_num")]
    public string OrderNum { get; set; }

    [JsonProperty("payment_method")]
    public string PaymentMethod { get; set; }

    [JsonProperty("po_number")]
    public object PoNumber { get; set; }

    [JsonProperty("product_transaction_id")]
    public string ProductTransactionId { get; set; }

    [JsonProperty("quick_invoice_id")]
    public object QuickInvoiceId { get; set; }

    [JsonProperty("reason_code_id")]
    public long ReasonCodeId { get; set; }

    [JsonProperty("recurring_id")]
    public object RecurringId { get; set; }

    [JsonProperty("response_message")]
    public object ResponseMessage { get; set; }

    [JsonProperty("return_date")]
    public object ReturnDate { get; set; }

    [JsonProperty("routing")]
    public object Routing { get; set; }

    [JsonProperty("settle_date")]
    public object SettleDate { get; set; }

    [JsonProperty("status_id")]
    public long StatusId { get; set; }

    [JsonProperty("tax")]
    public string Tax { get; set; }

    [JsonProperty("terminal_id")]
    public object TerminalId { get; set; }

    [JsonProperty("terminal_serial_number")]
    public object TerminalSerialNumber { get; set; }

    [JsonProperty("terms_agree")]
    public object TermsAgree { get; set; }

    [JsonProperty("tip_amount")]
    public string TipAmount { get; set; }

    [JsonProperty("transaction_amount")]
    public string TransactionAmount { get; set; }

    [JsonProperty("transaction_api_id")]
    public object TransactionApiId { get; set; }

    [JsonProperty("transaction_c1")]
    public object TransactionC1 { get; set; }

    [JsonProperty("transaction_c2")]
    public object TransactionC2 { get; set; }

    [JsonProperty("transaction_c3")]
    public object TransactionC3 { get; set; }

    [JsonProperty("transaction_code")]
    public object TransactionCode { get; set; }

    [JsonProperty("transaction_settlement_status")]
    public object TransactionSettlementStatus { get; set; }

    [JsonProperty("type_id")]
    public long TypeId { get; set; }

    [JsonProperty("verbiage")]
    public string Verbiage { get; set; }

    [JsonProperty("void_date")]
    public object VoidDate { get; set; }
}

public partial class Welcome
{
    public static Welcome FromJson(string json) => JsonConvert.DeserializeObject<Welcome>(json, Converter.Settings);
}

public static class Serialize
{
    public static string ToJson(this Welcome self) => JsonConvert.SerializeObject(self, Converter.Settings);
}

public class Converter
{
    public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
    {
        MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
        DateParseHandling = DateParseHandling.None,
    };
}

}

Ray Krungkaew
  • 6,652
  • 1
  • 17
  • 28