Solved

C# Parsing from a string with no delimers.

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

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

820 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