json parsing C# code example

Example 1: golang parsing xml

package main

import (
    "encoding/json"
    "encoding/xml"
    "fmt"
)

type Data struct {
    XMLName    xml.Name `xml:"data" json:"-"`
    PersonList []Person `xml:"person" json:"people"`
}

type Person struct {
    XMLName   xml.Name `xml:"person" json:"-"`
    Firstname string   `xml:"firstname" json:"firstname"`
    Lastname  string   `xml:"lastname" json:"lastname"`
    Address   *Address `xml:"address" json:"address,omitempty"`
}

type Address struct {
    City  string `xml:"city" json:"city,omitempty"`
    State string `xml:"state" json:"state,omitempty"`
}

func main() {
    rawXmlData := "<data><person><firstname>Nic</firstname><lastname>Raboy</lastname><address><city>San Francisco</city><state>CA</state></address></person><person><firstname>Maria</firstname><lastname>Raboy</lastname></person></data>"
    var data Data
    xml.Unmarshal([]byte(rawXmlData), &data)
    jsonData, _ := json.Marshal(data)
    fmt.Println(string(jsonData))
}

Example 2: c# parse json

Product product = new Product();
product.Name = "Apple";
product.Expiry = new DateTime(2008, 12, 28);
product.Price = 3.99M;
product.Sizes = new string[] { "Small", "Medium", "Large" };

string json = JsonConvert.SerializeObject(product);
//{
//  "Name": "Apple",
//  "Expiry": "2008-12-28T00:00:00",
//  "Price": 3.99,
//  "Sizes": [
//    "Small",
//    "Medium",
//    "Large"
//  ]
//}

Product deserializedProduct = JsonConvert.DeserializeObject<Product>(json);