Read JSON in C#
-
Hi, I have a web service which returns JSON string like this:
{
"valid": true,
"messages": [
"Cannot validate bank code length. No information available.",
"Cannot get BIC. No information available."
],
"iban": "BH00000000000000000000",
"bankData": {
"bankCode": "",
"name": ""
},
"checkResults": {
"bankCode": false
}
}I am able to get it using below code:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://openiban.com/validate/" + txtIBAN.Text.Trim());
try
{
WebResponse response = request.GetResponse();
using (Stream responseStream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);string strsb = reader.ReadToEnd(); }
}
catch (WebException ex)
{
WebResponse errorResponse = ex.Response;
using (Stream responseStream = errorResponse.GetResponseStream())
{
StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
String errorText = reader.ReadToEnd();
// log errorText
}
throw;
}but I want to know can I now read the value valid if it's rue or false? Thanks, Jassim[^]
Technology News @ www.JassimRahma.com
-
Hi, I have a web service which returns JSON string like this:
{
"valid": true,
"messages": [
"Cannot validate bank code length. No information available.",
"Cannot get BIC. No information available."
],
"iban": "BH00000000000000000000",
"bankData": {
"bankCode": "",
"name": ""
},
"checkResults": {
"bankCode": false
}
}I am able to get it using below code:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://openiban.com/validate/" + txtIBAN.Text.Trim());
try
{
WebResponse response = request.GetResponse();
using (Stream responseStream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);string strsb = reader.ReadToEnd(); }
}
catch (WebException ex)
{
WebResponse errorResponse = ex.Response;
using (Stream responseStream = errorResponse.GetResponseStream())
{
StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
String errorText = reader.ReadToEnd();
// log errorText
}
throw;
}but I want to know can I now read the value valid if it's rue or false? Thanks, Jassim[^]
Technology News @ www.JassimRahma.com
Use Json.NET[^] - you can add a reference via NuGet[^]:
Install-Package Newtonsoft.Json
You can then either use LINQ to JSON[^] to read the property:
JObject result = JObject.Parse(strsb);
bool isValid = (bool)result["valid"];or create strongly typed classes to represent the result:
public class ResponseData
{
public bool valid { get; set; }
public IList<string> messages { get; set; }
public string iban { get; set; }
public BankData bankData { get; set; }
public IDictionary<string, bool> checkResults { get; set; }
}public class BankData
{
public string bankCode { get; set; }
public string name { get; set; }
}...
var result = JsonConvert.DeserializeObject<ResponseData>(strsb);
bool isValid = result.valid;
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Hi, I have a web service which returns JSON string like this:
{
"valid": true,
"messages": [
"Cannot validate bank code length. No information available.",
"Cannot get BIC. No information available."
],
"iban": "BH00000000000000000000",
"bankData": {
"bankCode": "",
"name": ""
},
"checkResults": {
"bankCode": false
}
}I am able to get it using below code:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://openiban.com/validate/" + txtIBAN.Text.Trim());
try
{
WebResponse response = request.GetResponse();
using (Stream responseStream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);string strsb = reader.ReadToEnd(); }
}
catch (WebException ex)
{
WebResponse errorResponse = ex.Response;
using (Stream responseStream = errorResponse.GetResponseStream())
{
StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
String errorText = reader.ReadToEnd();
// log errorText
}
throw;
}but I want to know can I now read the value valid if it's rue or false? Thanks, Jassim[^]
Technology News @ www.JassimRahma.com
To add to Richard's answer, you can use this free handy-dandy online tool to generate C# classes from JSON. You may want to rename the generated class names to be more developer friendly. json2csharp - generate c# classes from json[^] :cool: /ravi
My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com
-
To add to Richard's answer, you can use this free handy-dandy online tool to generate C# classes from JSON. You may want to rename the generated class names to be more developer friendly. json2csharp - generate c# classes from json[^] :cool: /ravi
My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com
Visual Studio already has a "Paste JSON as classes" option, since 2012.2: ‘Paste JSON As Classes’ in ASP.NET and Web Tools 2012.2 RC | .NET Web Development and Tools Blog[^] The output isn't perfect, but it's usually a good place to start. :)
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Hi, I have a web service which returns JSON string like this:
{
"valid": true,
"messages": [
"Cannot validate bank code length. No information available.",
"Cannot get BIC. No information available."
],
"iban": "BH00000000000000000000",
"bankData": {
"bankCode": "",
"name": ""
},
"checkResults": {
"bankCode": false
}
}I am able to get it using below code:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://openiban.com/validate/" + txtIBAN.Text.Trim());
try
{
WebResponse response = request.GetResponse();
using (Stream responseStream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);string strsb = reader.ReadToEnd(); }
}
catch (WebException ex)
{
WebResponse errorResponse = ex.Response;
using (Stream responseStream = errorResponse.GetResponseStream())
{
StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
String errorText = reader.ReadToEnd();
// log errorText
}
throw;
}but I want to know can I now read the value valid if it's rue or false? Thanks, Jassim[^]
Technology News @ www.JassimRahma.com
When I look at the message text above, I think you can get confused by their convention. Their
"valid": true
seems to mean that the message you sent them was syntactically correct. But you are likely interested in the IBAN. And that's given in a different part of the message:"checkResults": { "bankCode": false }
. Well, I tried that service with my IBAN, and got following result (I edited the number on the response below):{
"valid": true,
"messages": [],
"iban": "DE...........",
"bankData": {
"bankCode": "",
"name": ""
},
"checkResults": {}
}WTF... A valid IBAN is indicated by an empty
checkresult
and an emptymessages
array.