2

Can anyone help me to parse this JSON into an object IN C# 4.0. I have spent the last two days trying.
I have JSON.NET and several other peoples suggestions to no avail.

I thought it would be best just to give the JSON sample and to ask for your suggestions.

{
"message-count":"1",
"messages":[
        {"to":"441234567890",
          "messageprice":"0.02900000",
          "status":"0",
          "messageid":"030000001DFE2CB1",
          "remainingbalance":"1.56500000",
                      "network":"23433"}
                ]
}

Many thanks, Adrian

p.s Their is some nice code here, if you want to use github. https://github.com/lukesampson/HastyAPI.Nexmo

5
  • First of all, I'd recommend avoiding the dash in JSON keys: "message-count" will cause you extra work. Use "message_count" Commented Jul 30, 2013 at 11:27
  • Hi AD, I have no control over this. This is what I get from the server. I have managed to sort it out. I just had the class structure wrong when using JSON.Net. It seems to take quite a while to load though. Why does Microsoft not have a reasonable introduction to JSON? Commented Jul 30, 2013 at 11:41
  • "message-count":"1" ? Commented Jul 30, 2013 at 11:42
  • @ADNow: why do you suggest to not use a dash in a property name? Is it because the C# language does not allow it (and thus require an extra attribute on the class)? Commented Jul 30, 2013 at 12:15
  • You cannot use dot syntax to reference the hyphenated keys in JavaScript, like data.message-count, you are forced to use data["message-count"]. I do not see Google suggesting to use dashes in their guidelines either: google-styleguide.googlecode.com/svn/trunk/… Commented Jul 30, 2013 at 12:26

2 Answers 2

2

I will cheat and create C# classes quickly using this tool: http://json2csharp.com/ (or just discovered http://jsonclassgenerator.codeplex.com/)

enter image description here

Then I change C# classes to my liking

public class MessagesJSON
{
    public int MessageCount { get; set; }
    public List<Message> Messages { get; set; }
}

public class Message
{
    public string To { get; set; }
    public double MessagePrice { get; set; }
    public int Status { get; set; }
    public string MessageId { get; set; }
    public double RemainingBalance { get; set; }
    public string Network { get; set; }
}

MessagesJSON is just a name I made that represents the JSON object that you are passing to C#.

I pass the JSON string from the client, e.g.

{\"MessageCount\":1,\"Messages\":[{\"To\":\"441234567890\",\"MessagePrice\":0.029,\"Status\":0,\"MessageId\":\"030000001DFE2CB1\",\"RemainingBalance\":1.565,\"Network\":\"23433\"}]

Then I can use JSON.NET to convert JSON to C# objects:

public void YourMethod(MessagesJSON json) {
   var result = JsonConvert.DeserializeObject<MessagesJSON>(json);
}

Here's the result:

enter image description here

Watch out for capitalisation.

If you want to use lower-case JSON keys only, change the C# classes to lower-case, e.g. public double messageprice { get; set; }

C# classes:

 public class MessagesJSON
    {
        public int message_count { get; set; }
        public List<Message> messages { get; set; }
    }

public class Message
{
    public string to { get; set; }
    public string messageprice { get; set; }
    public string status { get; set; }
    public string messageid { get; set; }
    public string remainingbalance { get; set; }
    public string network { get; set; }
}

This is as close to your JSON as you want:

{\"message_count\":1,\"messages\":[{\"to\":\"441234567890\",\"messageprice\":\"0.02900000\",\"status\":\"0\",\"messageid\":\"030000001DFE2CB1\",\"remainingbalance\":\"1.56500000\",\"network\":\"23433\"}]}

enter image description here

or use one of these solutions if you really like CamelCasing:

CamelCase only if PropertyName not explicitly set in Json.Net?

JObject & CamelCase conversion with JSON.Net

I myself prefer attributes

public class Message
    {
        [JsonProperty("to")]
        public string To { get; set; }

        [JsonProperty("messageprice")]
        public string MessagePrice { get; set; }

        [JsonProperty("status")]
        public string Status { get; set; }

        [JsonProperty("messageid")]
        public string MessageId { get; set; }

        [JsonProperty("remainingbalance")]
        public string RemainingBalance { get; set; }

        [JsonProperty("network")]
        public string Network { get; set; }
    }

Pass your string:

"{\"message_count\":1,\"messages\":[{\"to\":\"441234567890\",\"messageprice\":\"0.02900000\",\"status\":\"0\",\"messageid\":\"030000001DFE2CB1\",\"remainingbalance\":\"1.56500000\",\"network\":\"23433\"}]}"

but get the pretty C# property names:

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Hi Ad, That's exactly what I did. Thanks a lot.
0

Create objects with the same structure as the json and call.

JsonConvert.DeserializeObject<Entity>(json);

Edit. You have to use JSON.NET if u wanna do it this way.

1 Comment

Thanks Tan. That's what I did do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.