Click here to Skip to main content
15,867,765 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using System;
using System.Net;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;



public partial class Food1 : System.Web.UI.Page
{
    string selectSQL;

    SqlCommand cmd = new SqlCommand();
     SqlCommand cmd1 = new SqlCommand();
    SqlConnection dbConn = new SqlConnection();
    SqlConnection dbConn1 = new SqlConnection();
    SqlConnection dbConn2 = new SqlConnection();
    SqlConnection dbConn3 = new SqlConnection();
    SqlConnection dbConn4 = new SqlConnection();
    SqlDataReader dr;
    public string dbstring = WebConfigurationManager.ConnectionStrings["DB_SCH"].ConnectionString;
    public SqlConnection MyConnection, MyConnection1;

    Stream st;

   protected void Page_Load(object sender, EventArgs e)
    {

string url ="https://stageserv.interswitchng.com/test_paydirect/api/v1/gettransaction.json?";
    productid.Value= "6205";

      transactionreference.Value = "7654325";
       
      amount.Value = "16700000";

      string mac =  "D3D1D05AFE42AD50818167EAC73C109168A0F108F32645C8B59E897FA930DA44F9230910DAC9E20641823799A107A02068F7BC0F4CC41D2952E249552255710F";
       
           // https://stageserv.interswitchng.com/test_paydirect/api/v1/gettransaction.json?productid=6205&transactionreference=1234567&amount=3000000 
//string ss ="https://stageserv.interswitchng.com/test_paydirect/api/v1/gettransaction.json?productid=21&transactionreference=8421941122&amount=300000";
//string ft =  "username=" + amount.Value+"&"+"username=" + amount.Value;
string now = url+"productid=" + productid.Value+"&"+"transactionreference=" + transactionreference.Value+"&"+"amount=" + amount.Value;


//Product_id.Value=(Convert.ToString(productid.Value)).ToString()+(Convert.ToString(transactionreference.Value)).ToString()+(Convert.ToString(amount.Value)).ToString();
string m_Input =(Convert.ToString(productid.Value)).ToString()+(Convert.ToString(transactionreference.Value)).ToString()+mac;

 


  Session["u"] = now;
//Response.Write(m_Input);
// Convert a string to byte
byte[] data = Encoding.UTF8.GetBytes(m_Input );
byte[] hash;
SHA512 shaM = new SHA512Managed();
 
// Convert byte to SHA512
hash = shaM.ComputeHash(data);
 
 
// Append bytes
StringBuilder result = new StringBuilder(hash.Length * 2);
for (int i = 0; i < hash.Length; i++)
result.Append(hash[i].ToString(false ? "X2" : "x2"));
 


Session["rr"] =result;
Response.Write(result+"<hr>");
//string bs = (Convert.ToString(Session["rr"])).ToString();

     
  
 
     }
protected void Button2_Click(object sender, EventArgs e)
    {

        Response.Write("URL <hr>"+Session["u"].ToString());

        try
        {
            var request = (HttpWebRequest)WebRequest.Create(Session["u"].ToString());
            request.Headers.Add("Hash", Session["rr"].ToString());
            var response = (HttpWebResponse)request.GetResponse();
            Stream dataStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(dataStream);
            // Read the content.
            string responseFromServer = reader.ReadToEnd();

            Response.Write("<br>Result<br>" + responseFromServer.ToString() + "<br>Result<br>");
        }
        catch (Exception et)
        {
            Response.Write("Error "+et.ToString());
        }
        

    }
} 



The response i get is

{"Amount":16700000,"CardNumber":"0095","MerchantReference":"7654325","PaymentReference":"FBN|WEB|WEBP|3-02-2016|170619","RetrievalReferenceNumber":"000000088836","LeadBankCbnCode":null,"LeadBankName":null,"SplitAccounts":[],"TransactionDate":"2016-02-03T16:41:43.923","ResponseCode":"00","ResponseDescription":"Approved Successful"}



how do i get the values of Transaction Date,ResponseDescription and Transaction date in c sharp ,please i need the code

What I have tried:

values frm a JSON string in C#
Posted
Updated 6-Mar-16 11:45am
v2

C#
1 - Download the Newtonsoft JSON DLL (or set up a Nuget fetch)
2 - Add the reference to your project
3 - Create a class (Transaction) with properties that match the names of the JSON fields.

Conversion becomes:

Transaction t = Json.JsonConvert.DeserializeObject<Transaction>(the JSON string)

Examples and more detail on Newtonoft's site.


Deserialize an Object[^]

In response to "don't understand" comment.


1 - The .Net framework doesn't (yet) have a library that provides easy translation from JSON string to C# object.
2 - Newtonsoft (follow link above to website and have poke around) provide a DLL (library). It is free to download. Download it. Get the right version for the version of .Net that you are using.
3 - Add a reference to the Newtonsoft DLL to your project, see MSDN if you don't know how to do this.

To use it:

We'll take just the first few fields in your JSON string to make the example simple.

C#
{'Amount':16700000,'CardNumber':'0095','MerchantReference':'7654325'}


Create a class Transaction (or any other name that you choose) that has properties that match the names of the fields in your JSON string.

C#
Class Transaction {
  Public Long Amount {get; set;}
  Public String CardNumber {get; set;}
  Public String MerchantReference {get; set;}
}

And the code to convert is...

C#
String json = @"{'Amount':16700000,'CardNumber':'0095','MerchantReference':'7654325'}";
Transaction t = Json.JsonConvert.DeserializeObject<Transaction>(json);


And the transaction object t will have the values you want.

C#
Console.WriteLine(t.CardNumber);
Console.WriteLine(t.Amount);
Console.WriteLine(t.MerchantReference);
 
Share this answer
 
v3
Comments
Member 11307750 6-Mar-16 17:47pm    
public class Help
{

public string ResponseDescription { get; set; }


}

String json = responseFromServer.ToString();
Help h = Json.JsonConvert.DeserializeObject<help>(json);

Console.WriteLine(h.ResponseDescription);- error on this line
Invalid token '(' in class, struct, or interface member declaration
cigwork 7-Mar-16 14:11pm    
That is never going to work.

Please go back and re-read what I wrote; especially the sentence, "Create a class Transaction (or any other name that you choose) that has properties that match the names of the fields in your JSON string." then go back and look at what you have written.
Member 11307750 7-Mar-16 17:47pm    
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Script.Serialization;
using Newtonsoft.Json;
using System.Net.Http;


public partial class My_Default2 : System.Web.UI.Page
{



public class Transaction
{
public Long Amount {get; set;}
public String CardNumber {get; set;}
public String MerchantReference {get; set;}
}
String json = @"{'Amount':16700000,'CardNumber':'0095','MerchantReference':'7654325'}";
Transaction t = Json.JsonConvert.DeserializeObject<Transaction>(json);
Console.WriteLine(t.CardNumber); - error on this line -invalid token '(' in class, struct, or interface member declaration
Console.WriteLine(t.Amount);
Console.WriteLine(t.MerchantReference);

protected void Page_Load(object sender, EventArgs e)
{


}



}
cigwork 8-Mar-16 13:51pm    
So the JSON string couldn't be parsed. Try googling JSON and "invalid token" and see what sort of problems give that exception. Then go back to the code and see what's wrong with the input string.

Suggest that you create a console project and just play around with hard coded JSON strings and converting them to .Net classes until you're comfortable with the tech. Then go back and try it in your main project.
public class Help
{
public int Amount { get; set; }
public object CardNumber { get; set; }



}

String json = responseFromServer.ToString();
Help h = Json.JsonConvert.DeserializeObject<help>(json);

Console.WriteLine(h.CardNumber); - error on this line
Invalid token '(' in class, struct, or interface member declaration
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900