Solved

C# Parsing from a string with no delimers.

Posted on 2016-09-16
2
28 Views
Last Modified: 2016-09-16
I am using c# and DotNetOpenAuth for the Auth2

I looking for recommendations on how parse this return string from company using REST API

The return string is {"Clean":true,"SetDefault":true,"SetReply":true,"Signatures":[{"Name":"Mahar Default","HTML":"&nbsp;\r\n<div><table>... Some Table Stuff ....</table>\r\n&nbsp;","Text":" Some Text Stuff\r\n","DefaultNew":true,"DefaultReply":false},{"Name":"Mahar Default Reply","HTML":"&nbsp;\r\n<div><table>... Some Table Stuff ....</table>\r\n&nbsp;","Text":" Some Text Stuff\r\n","DefaultNew":false,"DefaultReply":true}],"Images":[{"Url":"Images/Get/N443/P4","Name":"P4"},{"Url":"Images/Get?account=N443&image=i3&extension=jpg","Name":"i3.jpg"},{"Url":"https://i.xink.io/Images/Get/N443/i3.jpg","Name":"Images-Get-N443-i3.jpg"},{"Url":"https://img.xink.io/Images/Get/N443/i1.jpg","Name":"Images-Get-N443-i1.jpg"}]}


Broken up it looks like this.
{
  "Clean":true,
  "SetDefault":true,
  "SetReply":true,
  "Signatures":
  [
    {"Name":"Mahar Default",
     "HTML":"&nbsp;\r\n<div><table>... Some Table Stuff ....</table>\r\n&nbsp;",
     "Text":" Some Text Stuff\r\n",
     "DefaultNew":true,
     "DefaultReply":false
    },
    { "Name":"Mahar Default Reply",
     "HTML":"&nbsp;\r\n<div><table>... Some Table Stuff ....</table>\r\n&nbsp;",
     "Text":" Some Text Stuff\r\n","DefaultNew":false,
     "DefaultReply":true
    }
  ],
"Images":
  [
    {"Url":"Images/Get/N443/P4",
    "Name":"P4"},{"Url":"Images/Get?account=N443&image=i3&extension=jpg",
    "Name":"i3.jpg"},{"Url":"https://i.xink.io/Images/Get/N443/i3.jpg",
    "Name":"Images-Get-N443-i3.jpg"},{"Url":"https://img.xink.io/Images/Get/N443/i1.jpg",
    "Name":"Images-Get-N443-i1.jpg"}
  ]
}

How would you parse this string in C#?

Here is the only thing provided by the company.  http://xink.io/xink-developer-api/

Thanks.
0
Comment
Question by:edwardq
2 Comments
 
LVL 42

Accepted Solution

by:
zephyr_hex earned 500 total points
ID: 41802247
That is a JSON string.  
If you want a dynamic object, you can just deserialize the JSON string using JsonConvert.DeserializeObject();

Here is an example of that: http://www.newtonsoft.com/json/help/html/deserializeobject.htm

Or, better yet, you could convert it to a typed class and parse it.  Here's an online tool that helps with how your class should be structured (just paste your JSON in there):  http://json2csharp.com/

So, your code would like something like the following:

classes generated by the converter I linked above:
public class Signature
{
    public string Name { get; set; }
    public string HTML { get; set; }
    public string Text { get; set; }
    public bool DefaultNew { get; set; }
    public bool DefaultReply { get; set; }
}

public class Image
{
    public string Url { get; set; }
    public string Name { get; set; }
}

public class RootObject
{
    public bool Clean { get; set; }
    public bool SetDefault { get; set; }
    public bool SetReply { get; set; }
    public List<Signature> Signatures { get; set; }
    public List<Image> Images { get; set; }
}

Open in new window



then, to use the classes (where data is your json string):

RootObject publicFeed = new RootObject();

using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{ 
    data = reader.ReadToEnd();
    publicFeed = JsonConvert.DeserializeObject<RootObject>(data); 
}

Open in new window


There's more on the topic here: http://www.wadewegner.com/2012/08/generating-c-classes-from-json/
1
 
LVL 4

Author Comment

by:edwardq
ID: 41802287
Thanks.. This is new to me..  I have some reading to do..
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now