Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C# Parsing from a string with no delimers.

Posted on 2016-09-16
2
Medium Priority
?
58 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 44

Accepted Solution

by:
zephyr_hex (Megan) earned 2000 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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

722 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