Solved

C# Parsing from a string with no delimers.

Posted on 2016-09-16
2
35 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 (Megan) 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

810 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