I am trying to write a console application using c# 2.0 which will consume my webservice and return results from the webmethod.
In my C# code, I am able to get my values from the webmethod, please see below sample code:
// print results
try
{
Type objtype = Type.GetType(crisresult.ToString());
object obj = Activator.CreateInstance(objtype);
Object[] mArgs = new Object[methodArgs.Length + 1];
methodArgs.CopyTo(mArgs, 0);
mArgs.SetValue(obj, methodArgs.Length);
methodArgs = mArgs;
Object result = mi.Invoke(service, methodArgs);
Console.WriteLine(result);
}
catch (Exception e)
{
Console.WriteLine("Error invoking method '" + methodName + "'");
Console.WriteLine(e);
}
Console.WriteLine("Press enter to continue...");
Console.ReadLine();
Now in above code my results from my webmethod is returned perfectly in JSON Type so my Object result
have JSON type of values for example:
{"FullName":"Mr Mahesh Sharma","Miles":0,"TierStatus":"IO","TierMiles":0,"MilesExpiry":0,"ExpiryDate":"30/03/2012 00:00:00","AccessToken":"106C9FD143AFA6198A9EBDC8B9CC0FB2CE867356222D21D45B16BEEB9A7F390B5E226665851D6DB9","ActiveCardNo":"00300452124","PersonID":8654110}
Above result, I want to print in console with below format:
FullName: Mr Mahesh Sharma
Miles: 0
TierStatus: IO
TierMiles:0
MilesExpiry:0
ExpiryDate:31 March 2012
AccessToken: 106C9FD143AFA6198A9EBDC8B9CC0FB2CE867356222D21D45B16BEEB9A7F390B5E226665851D6DB9
ActiveCardNo: 00300452124
PersonID: 8654110
To print JSON nicely in JavaScript, you can use the JSON. stringify(obj, replacer, space) method. The third parameter of the JSON. stringify() method specifies the number of spaces to use when outputting the JSON string.
A JSONObject has few important methods to display the values of different types like getString() method to get the string associated with a key string, getInt() method to get the int value associated with a key, getDouble() method to get the double value associated with a key and getBoolean() method to get the boolean ...
print(response. json()) should give the the data formatted as JSON for this response.
log() or JSON. stringify() Method You can use the console. log() method, if you simply wants to know what's inside an object for debugging purpose. This method will print the object in browser console.
You can use the following:
return Newtonsoft.Json.JsonConvert.SerializeObject( obj, Formatting.Indented );
Simple and clean.
Personally I'd use JSON.NET. Sample code:
using System;
using Newtonsoft.Json.Linq;
class Test
{
static void Main()
{
string json = "{\"FullName\":\"Mr Mahesh Sharma\",\"Miles\":0,\"TierStatus\":\"IO\"," +
"\"TierMiles\":0,\"MilesExpiry\":0,\"ExpiryDate\":\"30/03/2012 00:00:00\"," +
"\"AccessToken\":\"106C9FD143AFA6198A9EBDC8B9CC0FB2CE867356222D21D45B16BEE" +
"B9A7F390B5E226665851D6DB9\",\"ActiveCardNo\":\"00300452124\",\"PersonID\":8654110}";
JObject parsed = JObject.Parse(json);
foreach (var pair in parsed)
{
Console.WriteLine("{0}: {1}", pair.Key, pair.Value);
}
}
}
Output:
FullName: "Mr Mahesh Sharma"
Miles: 0
TierStatus: "IO"
TierMiles: 0
MilesExpiry: 0
ExpiryDate: "30/03/2012 00:00:00"
AccessToken: "106C9FD143AFA6198A9EBDC8B9CC0FB2CE867356222D21D45B16BEEB9A7F390B5E226665851D6DB9"
ActiveCardNo: "00300452124"
PersonID: 8654110
Note that the value in each pair is a JToken
, which you can cast to the relevant type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With