user1855588
user1855588

Reputation: 77

Convert Array of KeyValue Pair To Json string

I have an array of objects coming from Service something like this

[0]= {
        Name=Only for Testing 
        soldTo=0039000000
        city=Testing
        State=IN
        address1=Testing
        address2=
        zip=5600
        country=US
     }

so may like this. I need to return this to Js so that i can parse and bind the values to UI controls

How to convert this array to Json string?

Upvotes: 1

Views: 1659

Answers (1)

atom.gregg
atom.gregg

Reputation: 1007

Because you have a class representing the data coming back from the service, if you don't mind returning the entire class properties to your client, then simply use one of the many json serializers out there. Start here: http://www.nuget.org/packages?q=json

If you want to reduce the number of fields that get serialized, then create your own class, convert the service object to your own class, and then serialize that using the same techniques.

Here is the Json.Net documentation showing how to serialize. http://james.newtonking.com/json/help/index.html

Update: Show conversion and serialization

Note: Uses ServiceStack serializer https://github.com/ServiceStack/ServiceStack.Text

// get data from the service
var serviceData = new List<ServiceObject>
{
    new ServiceObject { name = "one", soldTo = "00000123", city = "sydney", state = "nsw", addressLine1 = "king st", addressLine2 = "", zip = "0123", country = "australia" },
    new ServiceObject { name = "two", soldTo = "00000456", city = "melbourne", state = "vic", addressLine1 = "william st", addressLine2 = "", zip = "0456", country = "australia" },
    new ServiceObject { name = "three", soldTo = "00000789", city = "adelaide", state = "sa", addressLine1 = "county cres", addressLine2 = "", zip = "0789", country = "australia" }
};

// convert it to what you want to return
var jsData = (from row in serviceData
              select new JsObject
              {
                  name = row.name,
                  soldTo = row.soldTo,
                  address = new JsAddress
                  {
                      line1 = row.addressLine1,
                      line2 = row.addressLine2,
                      postCode = row.zip,
                      state = row.state,
                      country = row.country
                  }
              }).ToList();

// turn it into a json string
var json = JsonSerializer.SerializeToString(jsData);

// this will spit out the result when using Linqpad
json.Dump("json");

}

class ServiceObject
{
    public string name { get; set; }
    public string soldTo { get; set; }
    public string city { get; set; }
    public string state { get; set; }
    public string addressLine1 { get; set; }
    public string addressLine2 { get; set; }
    public string zip { get; set; }
    public string country { get; set; }
}

class JsObject
{
    public string name { get; set; }
    public string soldTo { get; set; }
    public JsAddress address { get; set; }
}

class JsAddress
{
    public string line1 { get; set; }
    public string line2 { get; set; }
    public string state { get; set; }
    public string postCode { get; set; }
    public string country { get; set; }

Cheers, Aaron

Upvotes: 1

Related Questions