Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C# parse json data

Posted on 2016-07-18
7
Medium Priority
?
111 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying 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

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…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

661 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