Solved

C# parse json data

Posted on 2016-07-18
7
56 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 63

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 63

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
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.

 

Author Comment

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

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 63

Expert Comment

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

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

828 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