Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print values from JSON Type Object to console in C#

Tags:

json

c#

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
like image 964
Manoj Singh Avatar asked Mar 02 '11 10:03

Manoj Singh


People also ask

How do I print a JSON object?

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.

How can we access values from JSON object?

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 ...

How do you print your response in JSON format?

print(response. json()) should give the the data formatted as JSON for this response.

Can you console log JSON?

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.


2 Answers

You can use the following:

return Newtonsoft.Json.JsonConvert.SerializeObject( obj, Formatting.Indented );

Simple and clean.

like image 101
JayKayOf4 Avatar answered Sep 24 '22 12:09

JayKayOf4


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.

like image 21
Jon Skeet Avatar answered Sep 25 '22 12:09

Jon Skeet