Solved

C# Parsing from a string with no delimers.

Posted on 2016-09-16
2
32 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

867 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

12 Experts available now in Live!

Get 1:1 Help Now