Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C# parse json data

Posted on 2016-07-18
7
Medium Priority
?
133 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 64

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 64

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
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 

Author Comment

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

Accepted Solution

by:
Fernando Soto earned 2000 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 64

Expert Comment

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

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

885 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