• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 147
  • Last Modified:

C# parse json data

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; }
  • 4
  • 3
1 Solution
Fernando SotoRetiredCommented:
How are you parsing the JSON string into the PersonResult object?
lrbristerAuthor Commented:

 List<PersonsResult> sg = new List<PersonsResult>();
 sg = JsonConvert.DeserializeObject<List<PersonsResult>>(data);
Fernando SotoRetiredCommented:
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?
7 new features that'll make your work life better

It’s our mission to create a product that solves the huge challenges you face at work every day. In case you missed it, here are 7 delightful things we've added recently to monday to make it even more awesome.

lrbristerAuthor Commented:
Store it in the public "mystring"
Fernando SotoRetiredCommented:
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

lrbristerAuthor Commented:
Sorry... simply forgot to apply points
Fernando SotoRetiredCommented:
Not a problem Irbrister, glad it worked out for you.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now