Link to home
Start Free TrialLog in
Avatar of Member_2_1242703
Member_2_1242703

asked on

JSON object to C#

I have a URL set up that will receive the following JSON object:

[
  {
    "user":"12345678-4321-1234-4321-123456789123",
    "entity_type":"user",
    "datetime":"2012-04-07T22:26:10.947Z"
  }
]

Open in new window


I need something that can receive this data, put each of the three above into a separate string, then "do something" with each string. How do I do this?
Avatar of Dirk Strauss
Dirk Strauss
Flag of South Africa image

First, install Newtonsoft.Json from NuGet. If your JSON is:
[
  {
    "user":"12345678-4321-1234-4321-123456789123",
    "entity_type":"user",
    "datetime":"2012-04-07T22:26:10.947Z"
  }
]

Open in new window

then create a class called whatever you like (perhaps myJsonClass) as follows:
public class myJsonClass
{
    public string user { get; set; }
    public string entity_type { get; set; }
    public string datetime { get; set; }
}

Open in new window

I find it easier to use an extension method to create objects from JSON, so I do this:
public static class ExtensionMethods
{
    public static T CreateObjectFrom_JSON<T>(this string value)
    {
        return JsonConvert.DeserializeObject<T>(value);
    }
}

Open in new window

NB: Seeing as you are using Newtonsoft.Json in the ExtensionMethods class, add the Nuget package to that project only if you create a separate project for your helper classes.

I then read the JSON string into a string variable (let's call it jsonString):
string jsonString = GetJSONStringFromWherever(); // Implementation is up to you

Open in new window

so then you can create an object of myJsonClass as follows:
myJsonClass myjsonObj = jsonString.CreateObjectFrom_JSON<myJsonClass>();

Open in new window

This is called deserialization. So once you have deserialized the JSON string back into your myJsonClass object, you can just use it in your code as you need to. No need to split it into separate strings.
Avatar of Member_2_1242703
Member_2_1242703

ASKER

So the final two pieces...the deserialization,
Where does this occur?
The NuGet package Netwtonsoft.Json does the serialization and deserialization. I just brought generics into the mix.
return JsonConvert.DeserializeObject<T>(value);

Open in new window

If you wanted to create a JSON string from an object, you would do the following (again an extension method, but you could create regular methods and call differently). You will notice that this extension method only acts on classes.:
public static string Create_JSON_FromObject<T>(this T value) where T : class
{
    return JsonConvert.SerializeObject(value, Formatting.Indented);
}

Open in new window

You could then create the JSON string from your class as follows:
myJsonClass cc = new myJsonClass();
cc.user = "Mike Miller";
cc.entity_type = "user";
cc.datetime = DateTime.Now.ToString(); // Not sure how you need your date formatted
string jsonString = cc.Create_JSON_FromObject();

Open in new window

The NuGet package Newtonsoft.Json is the key.
Sorry...

If I send this object to a URL, where is the code happening? Does it matter? Can I do this on a web form?
I have a URL set up that will receive the following JSON object:
I was under the impression that you have already written the code to read the JSON and that you have the JSON in a string variable.
See the line of code I added initially that read:
string jsonString = GetJSONStringFromWherever(); // Implementation is up to you

Open in new window

You would then need to do something like this in your web form:
string jsonString = "";
using (WebClient wc = new WebClient())
{
   jsonString = wc.DownloadString("www.whateveryourUrl.com");
}

Open in new window

I think this is in the System.Net namespace.
  • The class myJsonClass is a class on its own
  • The class ExtensionMethods is also a class on its own, only static
  • You would call CreateObjectFrom_JSON<myJsonClass>() after you have read the jsonString value as per above.
Sorry I wasn't more clear. I'm getting a JSON object sent to me, and I need to take those values and turn it into a string.
How does the final string need to look like?
At this point, it doesn't matter. I'm pretty new to this. I just want to be able to see the string in any format. I can play around with it to do what I need once I get going in the right direction.
ASKER CERTIFIED SOLUTION
Avatar of Dirk Strauss
Dirk Strauss
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Got it! Thanks for sticking with me
Awesome! You're welcome Mike! Glad you got it working.