Solved

C# parse json data

Posted on 2016-07-18
7
40 Views
Last Modified: 2016-07-22
I have inbound json data which I cast / pars as a list

In the following string I need to build comma separated string from the json "sub set???"

I essentially want the category string to be passed along as Category1,Category2, Category(n))

This is an example of the inbound string
"unique_arg_key":"unique_arg_value","category":["category1", "category2"],"reason":"500 No Such User"


I did a copy and paste special json data as classes and got this for my whole class
I added the "mystring" which I need to hold all of the category(n) values

public class PersonResult
{
    public string status { get; set; }
    public string sg_event_id { get; set; }
    public string sg_message_id { get; set; }
    public string _event { get; set; }
    public string email { get; set; }
    public int timestamp { get; set; }
    public string smtpid { get; set; }
    public string unique_arg_key { get; set; }
    public string[] category { get; set; }
    public string reason { get; set; }
    public string type { get; set; }
    public int asm_group_id { get; set; }
public mystring string {get; set; }
}
0
Comment
Question by:lrbrister
  • 4
  • 3
7 Comments
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 41717707
How are you parsing the JSON string into the PersonResult object?
0
 

Author Comment

by:lrbrister
ID: 41717711
NewtonSoft

 List<PersonsResult> sg = new List<PersonsResult>();
 sg = JsonConvert.DeserializeObject<List<PersonsResult>>(data);
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 41717720
That means that NewtonSoft is creating the class with an array of category. So are you saying you want to parse the category array into a comma separated string and store the results somewhere else?
0
DevOps Toolchain Recommendations

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

 

Author Comment

by:lrbrister
ID: 41717724
Store it in the public "mystring"
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 41717749
Hi lrbrister;

This should do what you need.
// Where personResult is an instance of the class and 
// workingSentence is there the string is being built and 
// the next is the next element of the array.
mystring = personResult.category.Aggregate((workingSentence, next) => workingSentence + ", " + next);

Open in new window

0
 

Author Closing Comment

by:lrbrister
ID: 41725090
Sorry... simply forgot to apply points
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 41725102
Not a problem Irbrister, glad it worked out for you.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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 is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

919 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

15 Experts available now in Live!

Get 1:1 Help Now