0

I want to convert this style in C#.I want to add Contacts.How can I do it?

My Json:

[{
    "Id": "1",
    "Name": "emre",
    "Country": "istanbul"
}, {
    "Id": "2",
    "Name": "semih",
    "Country": "siirt"
}]

I want this style:

{
    "contacts": [
        {       "Id": "1",
                "Name": "emre",
                "Country": "istanbul"
        },
       {       "Id": "2",
                "Name": "semih",
                "Country": "siirt"
        }

  ]
}

My web service:

    String resultJSON = "";
    JavaScriptSerializer js = new JavaScriptSerializer();

    try{
        Context.Response.Clear();
        Context.Response.ContentType = "application/json";

        con.Open();
        cmd = new SqlCommand("Select * from City", con);
        reader = cmd.ExecuteReader();
        dt = new DataTable();
            con.Close();



        JavaScriptSerializer serializer = new JavaScriptSerializer();
        List<Dictionary<String, Object>> tableRows = new List<Dictionary<string, object>>();
        Dictionary<String, Object> row;
        foreach (DataRow dr in dt.Rows)
        {
            row = new Dictionary<string, object>();
            foreach (DataColumn col in dt.Columns)
            {                     
              row.Add(col.ColumnName, dr[col].ToString());
            }
            tableRows.Add(row);
        }
            resultJSON = serializer.Serialize(tableRows).ToString();

    }
    catch (Exception ex)
    {
        resultJSON = ex.Message.ToString();

    }
    Context.Response.Write(resultJSON);

   // return resultJSON;

}

2 Answers 2

1

Instead of directly serializing the rows create a data model say class named Contact which has the different properties like Id , Name, country , fill it up from the database and use a library like JSON.net to serialize it .

Something similar should work for your datamodel. Please look the Serializing Collections from JSON.net

Product p1 = new Product
 {
  Name = "Product 1",
  Price = 99.95m,
  ExpiryDate = new DateTime(2000, 12, 29, 0, 0, 0, DateTimeKind.Utc),
 };
Product p2 = new Product
{
 Name = "Product 2",
 Price = 12.50m,
 ExpiryDate = new DateTime(2009, 7, 31, 0, 0, 0, DateTimeKind.Utc),
};
ProductList productList = new ProductList ();
p.Add(p1);
p.Add(p2);

string json = JsonConvert.SerializeObject(productList , Formatting.Indented);

class ProductList 
{
  Public List<Product> products{get;set;}
   // Add method or other methods you require for your collection

}
Sign up to request clarification or add additional context in comments.

2 Comments

@EmreAslan pls look the edited answer. Something similar would help
@EmreAslan pls mark it as answer if it has answered your question
0

Like Ashley John said, it would be smart to create a class representing the data json data structure. If you are lazy, or i doubt how it should look, you can use json2csharp to create the class.

Then use a json serializer of your choice, f.x. JSON.net as mentioned by Ashley J, to serialize the entire enumerable of Contacts at once. If you need the json value to be named 'contacts', you can create a containing class with field for the enumerable of contacts, so that when you serialize the containing class, you get the wanted result

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.