I'm trying to deserialize a Facebook friend's Graph API call into a list of objects. The JSON object looks like:
{"data":[{"id":"518523721","name":"ftyft"},
{"id":"527032438","name":"ftyftyf"},
{"id":"527572047","name":"ftgft"},
{"id":"531141884","name":"ftftft"},
{"id":"532652067","name"...
List<EFacebook> facebooks = new JavaScriptSerializer().Deserialize<List<EFacebook>>(result);
It's not working, because the primitive object is invalid. How can I deserialize this?
You need to create a structure like this:
public class Friends
{
public List<FacebookFriend> data {get; set;}
}
public class FacebookFriend
{
public string id {get; set;}
public string name {get; set;}
}
Then you should be able to do:
Friends facebookFriends = new JavaScriptSerializer().Deserialize<Friends>(result);
The names of my classes are just an example. You should use proper names.
Adding a sample test:
string json =
#"{""data"":[{""id"":""518523721"",""name"":""ftyft""}, {""id"":""527032438"",""name"":""ftyftyf""}, {""id"":""527572047"",""name"":""ftgft""}, {""id"":""531141884"",""name"":""ftftft""}]}";
Friends facebookFriends = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<Friends>(json);
foreach(var item in facebookFriends.data)
{
Console.WriteLine("id: {0}, name: {1}", item.id, item.name);
}
Produces:
id: 518523721, name: ftyft
id: 527032438, name: ftyftyf
id: 527572047, name: ftgft
id: 531141884, name: ftftft
Sometimes I prefer dynamic objects:
public JsonResult GetJson()
{
string res;
WebClient client = new WebClient();
// Download string
string value = client.DownloadString("https://api.instagram.com/v1/users/000000000/media/recent/?client_id=clientId");
// Write values
res = value;
dynamic dyn = JsonConvert.DeserializeObject(res);
var lstInstagramObjects = new List<InstagramModel>();
foreach(var obj in dyn.data)
{
lstInstagramObjects.Add(new InstagramModel()
{
Link = (obj.link != null) ? obj.link.ToString() : "",
VideoUrl = (obj.videos != null) ? obj.videos.standard_resolution.url.ToString() : "",
CommentsCount = int.Parse(obj.comments.count.ToString()),
LikesCount = int.Parse(obj.likes.count.ToString()),
CreatedTime = new System.DateTime(1970, 1, 1, 0, 0, 0, 0).AddSeconds((double.Parse(obj.created_time.ToString()))),
ImageUrl = (obj.images != null) ? obj.images.standard_resolution.url.ToString() : "",
User = new InstagramModel.UserAccount()
{
username = obj.user.username,
website = obj.user.website,
profile_picture = obj.user.profile_picture,
full_name = obj.user.full_name,
bio = obj.user.bio,
id = obj.user.id
}
});
}
return Json(lstInstagramObjects, JsonRequestBehavior.AllowGet);
}
A great way to automatically generate these classes for you is to copy your JSON output and throw it in here:
http://json2csharp.com/
It will provide you with a starting point to touch up your classes for deserialization.
Very easily we can parse JSON content with the help of dictionary and JavaScriptSerializer. Here is the sample code by which I parse JSON content from an ashx file.
var jss = new JavaScriptSerializer();
string json = new StreamReader(context.Request.InputStream).ReadToEnd();
Dictionary<string, string> sData = jss.Deserialize<Dictionary<string, string>>(json);
string _Name = sData["Name"].ToString();
string _Subject = sData["Subject"].ToString();
string _Email = sData["Email"].ToString();
string _Details = sData["Details"].ToString();
Newtonsoft.JSON is a good solution for these kind of situations. Also Newtonsof.JSON is faster than others, such as JavaScriptSerializer, DataContractJsonSerializer.
In this sample, you can the following:
var jsonData = JObject.Parse("your JSON data here");
Then you can cast jsonData to JArray, and you can use a for loop to get data at each iteration.
Also, I want to add something:
for (int i = 0; (JArray)jsonData["data"].Count; i++)
{
var data = jsonData[i - 1];
}
Working with dynamic object and using Newtonsoft serialize is a good choice.
I agree with Icarus (would have commented if I could),
but instead of using a CustomObject class,
I would use a Dictionary (in case Facebook adds something).
private class MyFacebookClass
{
public IList<IDictionary<string, string>> data { get; set; }
}
or
private class MyFacebookClass
{
public IList<IDictionary<string, object>> data { get; set; }
}
Serialization:
// Convert an object to JSON string format
string jsonData = JsonConvert.SerializeObject(obj);
Response.Write(jsonData);
Deserialization::
To deserialize a dynamic object
string json = #"{
'Name': 'name',
'Description': 'des'
}";
var res = JsonConvert.DeserializeObject< dynamic>(json);
Response.Write(res.Name);
If you're using .NET Core 3.0, you can use System.Text.Json (which is now built-in) to deserialize JSON.
The first step is to create classes to model the JSON. There are many tools which can help with this, and some of the answers here list them.
Some options are http://json2csharp.com, http://app.quicktype.io, or use Visual Studio (menu Edit → Paste Special → Paste JSON as classes).
public class Person
{
public string Id { get; set; }
public string Name { get; set; }
}
public class Response
{
public List<Person> Data { get; set; }
}
Then you can deserialize using:
var people = JsonSerializer.Deserialize<Response>(json);
If you need to add settings, such as camelCase handling, then pass serializer settings into the deserializer like this:
var options = new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase };
var person = JsonSerializer.Deserialize<Response>(json, options);
You can use this extensions
public static class JsonExtensions
{
public static T ToObject<T>(this string jsonText)
{
return JsonConvert.DeserializeObject<T>(jsonText);
}
public static string ToJson<T>(this T obj)
{
return JsonConvert.SerializeObject(obj);
}
}
Here is another site that will help you with all the code you need as long as you have a correctly formated JSON string available:
https://app.quicktype.io/
Related
I have write a C# Program to Generate "Json" File for Eway Bill Uploading
My Json Format is :- (Its not working When I upload in EwayBill)
Please help me,how to do this
[{"Supply Type":"Outward","Sub Type":"Export","Doc Type":"Tax Invoice","Doc No":"PK/18/0015","Doc Date":"16/02/2018 00:00:00","From_OtherPartyName":"KH Exports India Private Limited Glove Division","From_GSTIN":"33AAACR1714R1ZA","From_Address1":"142/1,Trunk Road","From_Address2":"Perumugai","From_Place":"Vellore","From_Pin Code":"632009","From_State":"Tamil Nadu","To_OtherPartyName":"K H EXPORTS INDIA PRIVATE LIMITED","To_GSTIN":"33AAACR1714R1ZA","To_Address1":"GLOVE DIVISION, GODOWN","To_Address2":"NEW NO. 24, KUMARAPPA STREET,","To_Place":"Chennai","To_Pin Code":"600003","To_State":"Tamil Nadu","Product":"FINISHED LEATHER GLOVES FOR LADIES","Description":"","HSN":"42032920","Unit":"PAIRS","Qty":"25","Assessable Value":"678","Tax Rate (S+C+I+Cess)":"9+9+0+0","CGST Amount":"6102","SGST Amount":"6102","IGST Amount":"0","CESS Amount":"0","Trans Mode":"Road","Distance (Km)":"115","Trans Name":"","Trans ID":"","Trans DocNo":"","Trans Date":"","Vehicle No":"TN23CB8274","Errors List":""}]
The below json is working fine (I get this json from eway bill website)
{
"version":"1.0.0123",
"billLists":[{
"userGstin":"29BQSPA3829E124",
"supplyType":"O",
"subSupplyType":1,
"docType":"INV",
"docNo":"1234",
"docDate":"04/03/2017",
"fromGstin":"29BQSPA3829E124",
"fromTrdName":"HUKKERI PVT LTD",
"fromAddr1":"OLD AIRPORT ROAD",
"fromAddr2":"OLD AIRPORT ROAD",
"fromPlace":"BANGALORE",
"fromPincode":560090,
"fromStateCode":29,
"toGstin":"29AAACG0569P1Z3",
"toTrdName":"AMBUJA PVT LTD",
"toAddr1":"MG ROAD",
"toAddr2":"MG ROAD",
"toPlace":"BANGALORE",
"toPincode":560090,
"toStateCode":29,
"totalValue":678,
"cgstValue":6102,
"sgstValue":6102,
"igstValue":0,
"cessValue":0,
"transMode":1,
"transDistance":567,
"transporterName":"",
"transporterId":"",
"transDocNo":"",
"transDocDate":"",
"vehicleNo":"KA12KA1234",
"itemList":[{
"itemNo":1,
"productName":"STEEL",
"productDesc":"STEEL",
"hsnCode":26180000,
"quantity":0,
"qtyUnit":"KGS",
"taxableAmount":678,
"sgstRate":9,
"cgstRate":9,
"igstRate":0,
"cessRate":0
}
]
}
]
}
I have tried below but its not solved...please check this code...
public bool WriteJason(DataTable dt, string path)
{
try
{
System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
List<Dictionary<string, string>> rows = new List<Dictionary<string, string>>();
Dictionary<string, string> row = null;
foreach (DataRow dr in dt.Rows)
{
row = new Dictionary<string, string>();
foreach (DataColumn col in dt.Columns)
{
row.Add(col.ColumnName.Trim().ToString(), Convert.ToString(dr[col]));
}
rows.Add(row);
}
string jsonstring = serializer.Serialize(rows);
using (var file = new StreamWriter(path, false))
{
file.Write(jsonstring);
file.Close();
file.Dispose();
}
return true;
}
catch { return false; }
}
As #mcy mentionned in a comment, you'll have an easier time using Json.NET.
To expand a little on this idea, I would personally take this approach:
Define a model that matches your Json-structure:
private class JsonModel
{
public string Version { get; set; }
public IList<JsonBillModel> BillLists { get; set; } = new List<JsonBillModel>();
}
private class JsonBillModel
{
public string UserGstin { get; set; }
public string SupplyType { get; set; }
public int SubSupplyType { get; set; }
public string DocType { get; set; }
//...
}
Create an instance of your model
var model = new JsonModel
{
Version = "1.0.0123",
BillLists =
{
new JsonBillModel
{
UserGstin = "29BQSPA3829E124",
SupplyType = "O",
SubSupplyType = 1,
DocType = "INV"
//...
}
}
};
Serialze your model to Json using Json.NET:
var serializerSettings = new JsonSerializerSettings();
serializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
var json = JsonConvert.SerializeObject(model, serializerSettings);
File.WriteAllText(path, json);
The reason we create an instance of JsonSerializerSettings with a CamelCasePropertyNamesContractResolver contract resolver is that our model would otherwise generate PascalCase keys in our Json (since our model is written in a PascalCase-format. If this sounds confusing, try leaving out the ContractResolver-line and see the result for yourself.
Write a test to ensure that your Json output matches your desired output, using your favourite testing library.
Sidenote: you can also use Json.NET to convert json into an instance of your model.
var model = JsonConvert.DeserializeObject<JsonModel>(json);
You have 2 option to create a JSON file.
using text editor like notepad
using online tools like JSON parser
Create a file from the JSON URL
if you want full process it provide in Learning Container.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Facebook;
using Newtonsoft.Json;
namespace facebook
{
class Program
{
static void Main(string[] args)
{
var client = new FacebookClient(acc_ess);
dynamic result = client.Get("fql", new { q = "select target_id,target_type from connection where source_id = me()"});
string jsonstring = JsonConvert.SerializeObject(result);
//jsonstring {"data":[{"target_id":9503123,"target_type":"user"}]}
List<RootObject> datalist = JsonConvert.DeserializeObject<List<RootObject>>(jsonstring);
}
public class Datum
{
public Int64 target_id { get; set; }
public string target_type { get; set; }
}
public class RootObject
{
public List<Datum> data { get; set; }
}
}
}
Cannot deserialize the current JSON object (e.g. {"name":"value"})
into type
'System.Collections.Generic.List`1[facebook.Program+RootObject]'
because the type requires a JSON array (e.g. [1,2,3]) to deserialize
correctly. To fix this error either change the JSON to a JSON array
(e.g. [1,2,3]) or change the deserialized type so that it is a normal
.NET type (e.g. not a primitive type like integer, not a collection
type like an array or List) that can be
I looked at other posts.
My json looks like this:
{"data":[{"target_id":9503123,"target_type":"user"}]}
To make it clear, in addition to #SLaks' answer, that meant you need to change this line :
List<RootObject> datalist = JsonConvert.DeserializeObject<List<RootObject>>(jsonstring);
to something like this :
RootObject datalist = JsonConvert.DeserializeObject<RootObject>(jsonstring);
As the error message is trying very hard to tell you, you can't deserialize a single object into a collection (List<>).
You want to deserialize into a single RootObject.
Can you try to change your json without data key like below?
[{"target_id":9503123,"target_type":"user"}]
That happened to me too, because I was trying to get an IEnumerable but the response had a single value. Please try to make sure it's a list of data in your response. The lines I used (for api url get) to solve the problem are like these:
HttpResponseMessage response = await client.GetAsync("api/yourUrl");
if (response.IsSuccessStatusCode)
{
IEnumerable<RootObject> rootObjects =
awaitresponse.Content.ReadAsAsync<IEnumerable<RootObject>>();
foreach (var rootObject in rootObjects)
{
Console.WriteLine(
"{0}\t${1}\t{2}",
rootObject.Data1, rootObject.Data2, rootObject.Data3);
}
Console.ReadLine();
}
Hope It helps.
The real problem is that you are using dynamic return type in the FacebookClient Get method. And although you use a method for serializing, the JSON converter cannot deserialize this Object after that.
Use insted of:
dynamic result = client.Get("fql", new { q = "select target_id,target_type from connection where source_id = me()"});
string jsonstring = JsonConvert.SerializeObject(result);
something like that:
string result = client.Get("fql", new { q = "select target_id,target_type from connection where source_id = me()"}).ToString();
Then you can use DeserializeObject method:
var datalist = JsonConvert.DeserializeObject<List<RootObject>>(result);
Hope this helps.
public partial class tree
{
public int id { get; set; }
public string name { get; set; }
public string sciencename { get; set; }
public int familyid { get; set; }
}
private async void PopulateDataGridView()
{
//For Single object response
tree treeobj = new tree();
treeobj = JsonConvert.DeserializeObject<tree>(Response);
//For list of object response
List<tree> treelistobj = new List<tree>();
treelistobj = JsonConvert.DeserializeObject<List<tree>>(Response);
//done
}
Below JSON data i'm trying to deserialize using JavaScriptSerializer.Deserialize but, am getting below exception.
Code:
var jsonSerialiser = new JavaScriptSerializer();
FCMCasepackVM casepack = jsonSerialiser.Deserialize<FCMCasepackVM>(selectedCasePack);
JSON Data:
{ P_ID:"1478952", P_NM:"BAHLSEN COOKIES WAFER ROLL MILK CHOCOLATE 3.5
OZ", BYR_ID:191, BYR_NM:"BYR_NM", VDR_ID:48532, VDR_NM:"KEHE FOOD DIST
INC", CPK_ID:"1478952-12", CPK_DSC:"BAHLSEN COOKIES WAFER ROLL MILK
CHOCOLAT", VDR_IT_CD_ID:"6398", UPC_ID:7056921950.0,
CPK_PRI_FLG:"True", CPK_SRC:"PEF", **FCM_RGLT_TYP_CT:"FSV"P"**,
CPK_P_ORIG_DSC:"", RGLN_EXCL_FLG:"False", BRND_TYP:"" }
Exception
Additional information: Cannot convert object of type 'System.String'
to type 'Meijer.MerchProduct.FCM.Models.ViewModels.FCMCasepackVM'
Even I tried serializing the data before deserialize however getting a different exception
Code:
var jsonSerialiser = new JavaScriptSerializer();
selectedCasePack = jsonSerialiser.Serialize(selectedCasePack);
FCMCasepackVM casepack = jsonSerialiser.Deserialize<FCMCasepackVM>(selectedCasePack);
JSON Data after serialize
"{ P_ID:\"1478952\", P_NM:\"BAHLSEN COOKIES WAFER ROLL MILK CHOCOLATE
3.5 OZ\", BYR_ID:191, BYR_NM:\"BYR_NM\", VDR_ID:48532, VDR_NM:\"KEHE FOOD DIST INC\", CPK_ID:\"1478952-12\", CPK_DSC:\"BAHLSEN COOKIES
WAFER ROLL MILK CHOCOLAT\", VDR_IT_CD_ID:\"6398\",
UPC_ID:7056921950.0, CPK_PRI_FLG:\"True\", CPK_SRC:\"PEF\",
FCM_RGLT_TYP_CT:\"FSV\"P\", CPK_P_ORIG_DSC:\"\", RGLN_EXCL_FLG:\"False\", BRND_TYP:\"\" }"
Exception
Additional information: Invalid object passed in, ':' or '}' expected.
(319): { P_ID:"1478952", P_NM:"BAHLSEN COOKIES WAFER ROLL MILK
CHOCOLATE 3.5 OZ", BYR_ID:191, BYR_NM:"BYR_NM", VDR_ID:48532,
VDR_NM:"KEHE FOOD DIST INC", CPK_ID:"1478952-12", CPK_DSC:"BAHLSEN
COOKIES WAFER ROLL MILK CHOCOLAT", VDR_IT_CD_ID:"6398",
UPC_ID:7056921950.0, CPK_PRI_FLG:"True", CPK_SRC:"PEF",
FCM_RGLT_TYP_CT:"FSV"P", CPK_P_ORIG_DSC:"", RGLN_EXCL_FLG:"False",
BRND_TYP:"" }
can anyone please help me?
Thanks in advance,
Karthik
The JSON you are using is invalid and without knowing your container object of FCMCasepackVM and how you got the JSON it is hard to tell what went wrong where. Here is a simple example using the built in System.Web.Script.Serialization technique with the JavaScriptSerializer.
public class POC
{
public int Id { get; set; }
public string Desc { get; set; }
}
static List<POC> GetPOCOs()
{
return new List<POC>
{
new POC { Id = 1, Desc = "John"},
new POC { Id = 2, Desc = "Jane" },
new POC { Id = 3, Desc = "Joey" }
};
}
static void Main(string[] args)
{
var pocos = GetPOCOs();
var serializer = new JavaScriptSerializer();
var sjson = serializer.Serialize(pocos);
var djson = serializer.Deserialize<List<POC>>(sjson);
Console.ReadLine();
}
When I serialize to JSON in text it should look like this:
[{"Id":1,"Desc":"John"},{"Id":2,"Desc":"Jane"},{"Id":3,"Desc":"Joey"}]
Or a single item may be:
{"Id":1,"Desc":"John"}
NOT:
{Id:1,Desc:"John"}
Generally a key pair system is all JSON is but it needs the key surrounded with quotes to be able to know what it is. If you are ever in doubt, use an online JSON validator like http://jsonlint.com/
Where this json data is serialised? quotes should have escaped during serialisation.
This JSON data is invalid, if you have access to app serialising this json data you should change that to escape quotes in the string. (usually all library does that automatically during serialisation)
Update
Hi Karthik, see following code to serialise the objects in JSON format using newtonsoft's library.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var d = new DoTheWork();
d.SerializeSample();
}
}
public class Sample
{
public string Id { get; set; }
public string Description { get; set; }
}
public class DoTheWork
{
public string SerializeSample()
{
List<Sample> sampleList = new List<Sample>();
sampleList.Add(new Sample { Id = "1", Description = "Karthik" });
sampleList.Add(new Sample { Id = "1", Description = "Sujit" });
sampleList.Add(new Sample { Id = "1", Description = "John\"s" });
StringBuilder sb = new StringBuilder();
using (StringWriter sw = new StringWriter(sb))
{
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(sw, sampleList);
}
System.Diagnostics.Debug.Write(sb.ToString());
return sb.ToString();
}
}
Given below is the type of JSON response ,
{
"?xml":{
"#version":"1.0",
"#encoding":"iso-8859-1"
},
"xmlreport":{
"#title":"ABC: TEST Most Saved2",
"#dates":"Week of May 19,2013",
"columns":{
"column":[
{
"#name":"Page",
"#type":"dimension",
"#text":"Page"
},
{
"#name":"Events",
"#type":"metric",
"#hastotals":"true",
"#text":"Events"
}
]
},
"rows":{
"row":[
{
"#rownum":"1",
"cell":[
{
"#columnname":"page",
"#csv":"\"http://www.ABC.com/profile/recipebox\"",
"#text":"http://www.ABC.com/profile/recipebox"
},
{
"#columnname":"events",
"#percentage":"\"0.1%\"",
"#text":"489"
}
]
},
{
"#rownum":"2",
"cell":[
{
"#columnname":"page",
"#csv":"\"http://www.ABC.com/recipes/peanut-butter-truffle-brownies/c5c602e4-007b-43e0-aaab-2f9aed89524c\"",
"#text":"http://www.ABC.com/recip...c602e4-007b-43e0-aaab-2f9aed89524c"
},
{
"#columnname":"events",
"#percentage":"\"0.0%\"",
"#text":"380"
}
]
}
]
},
"totals":{
"pagetotals":{
"total":{
"#columnname":"events",
"#value":"1820.000000",
"#text":"1,820 (0.2%)"
}
},
"reporttotals":{
"total":{
"#columnname":"events",
"#value":"7838.000000",
"#text":"7,838 (0.8%)"
}
},
"timeperiodtotals":{
"total":{
"#columnname":"events",
"#value":"955774.000000",
"#text":"955,774 (100.0%)"
}
}
}
}
}
I am unable to parse the object.Could you please help me out how do I read the attributes and elements after parsing. I am using C#
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(XML);
string jsonText = JsonConvert.SerializeXmlNode(doc);
//var result = Newtonsoft.Json.JsonConvert.DeserializeXmlNode(jsonText, "xmlreport");
var results = JsonConvert.DeserializeObject<dynamic>(jsonText);
JToken token = JObject.Parse(jsonText);
var report = token["xmlreport"];
}
My understanding of the question is you've got some Xml and you need to send out json. Couple of points before we get to the code:
1) Don't convert xml to json directly as it causes issues
2) Parse the xml to objects at your end and then work out the format to return; decoupling what comes in and goes out will allow for one of the interfaces to change in the future without impacting the other as you can tweak the mapping
So, onto the code ...
Essentially parse the xml to objects to allow for further processing and then push out as json.
class Program
{
private static string starting =
"<xmlreport title=\"ABC: TEST Most Saved2\" dates=\"Week of May 19,2013\"><columns><column name=\"Page\" type=\"dimension\">Page</column><column name=\"Events\" type=\"metric\" hastotals=\"true\">Events</column></columns><rows><row rownum=\"1\"><cell columnname=\"page\" csv=\"http://www.ABC.com/profile/recipebox\">http://www.ABC.com/profile/recipebox</cell><cell columnname=\"events\" percentage=\"0.1%\">489</cell></row><row rownum=\"2\"><cell columnname=\"page\" csv=\"http://www.ABC.com/recipes/peanut-butter-truffle-brownies/c5c602e4-007b-43e0-aaab-2f9aed89524c\">http://www.ABC.com/recipes/peanut-butter-truffle-brownies/c5c602e4-007b-43e0-aaab-2f9aed89524c</cell><cell columnname=\"events\" percentage=\"0.0%\">380</cell></row></rows><totals><pagetotals><total columnname=\"events\" value=\"1820.00000\">1,820 (0.2%)</total></pagetotals><reporttotals><total columnname=\"events\" value=\"7838.000000\">7,838 (0.8%)</total></reporttotals><timeperiodtotals><total columnname=\"events\" value=\"955774.000000\">955,774 (100.0%)</total></timeperiodtotals></totals></xmlreport>";
static void Main(string[] args)
{
// parse from xml to objects
StringReader reader = new StringReader(starting);
XmlSerializer xmlSerializer = new XmlSerializer(typeof(XmlReport));
var xmlreport = (XmlReport)xmlSerializer.Deserialize(reader);
// todo: do some process mapping ...
// parse out as json
var json = JsonConvert.SerializeObject(xmlreport);
Console.WriteLine(json);
Console.ReadLine();
}
}
[Serializable]
[XmlRoot(ElementName = "xmlreport")]
public class XmlReport
{
[XmlAttribute(AttributeName = "title")]
public string Title { get; set; }
[XmlAttribute(AttributeName = "dates")]
public string Dates { get; set; }
[XmlArray(ElementName = "columns")]
[XmlArrayItem(typeof(Column), ElementName = "column")]
public Collection<Column> Columns { get; set; }
}
[Serializable]
public class Column
{
[XmlAttribute(AttributeName = "name")]
public string Name { get; set; }
[XmlAttribute(AttributeName = "type")]
public string Type { get; set; }
}
I've tried to parse the json to the original xml to begin with so appologies if I've not interpreted it properly. I've not done the entire structure but I hope the example above gives you an idea of how to do the rest.
Hope this helps.
One way of doing this is by getting the actual Data Structure of that JSON object that you have there.
Then create classes representing that data structure (if you can get a complete response -- having all properties, you just use this site to convert that to classes).
After that, deserialize that JSON object into your class using different libraries available. A sample could be this one.
Use this (JSon.NET) and a recursive function writes all keys and values to output window.
[TestMethod]
public void ParseMePlease()
{
string s = #"{""?xml"":{""#version"":""1.0"",""#encoding"":""iso-8859-1""},
""xmlreport"":{""#title"":""ABC: TEST Most Saved2"",""#dates"":""Week of May 19,2013"",
""columns"":{""column"":[{""#name"":""Page"",""#type"":""dimension"",""#text"":""Page""},{""#name"":""Events"",""#type"":""metric"",""#hastotals"":""true"",""#text"":""Events""}]},
""rows"":
{""row"":[{""#rownum"":""1"",""cell"":[{""#columnname"":""page"",""#csv"":""\""http://www.ABC.com/profile/recipebox\"""",""#text"":""http://www.ABC.com/profile/recipebox""},{""#columnname"":""events"",""#percentage"":""\""0.1%\"""",""#text"":""489""}]},
{""#rownum"":""2"",""cell"":[{""#columnname"":""page"",""#csv"":""\""http://www.ABC.com/recipes/peanut-butter-truffle-brownies/c5c602e4-007b-43e0-aaab-2f9aed89524c\"""",""#text"":""http://www.ABC.com/recip...c602e4-007b-43e0-aaab-2f9aed89524c""},{""#columnname"":""events"",""#percentage"":""\""0.0%\"""",""#text"":""380""}]}]},
""totals"":{""pagetotals"":{""total"":{""#columnname"":""events"",""#value"":""1820.000000"",""#text"":""1,820 (0.2%)""}},
""reporttotals"":{""total"":{""#columnname"":""events"",""#value"":""7838.000000"",""#text"":""7,838 (0.8%)""}},
""timeperiodtotals"":{""total"":{""#columnname"":""events"",""#value"":""955774.000000"",""#text"":""955,774 (100.0%)""}}}}}";
var result=JsonConvert.DeserializeObject<object>(s);
Debug.WriteLine("Right Click Result on quick watch for Result Views!(On Debug)"+result.ToString() );
JObject jobject = ((Newtonsoft.Json.Linq.JObject)result);
PrintDetail(jobject);
}
public void PrintDetail(JObject node)
{
foreach (var item in node)
{
Debug.WriteLine("Key:" + item.Key + " Value:" + item.Value);
if (item.Value is JObject)
{
PrintDetail((JObject)item.Value);
}
}
}
I want to be able to convert a List<T> into a specific JSON table-like format. In my case, the T will always be a simple object (no nested properties). Here are two examples to illustrate what I want.
Example #1: List<Person> to JSON
// C# list of Persons
var list = new List<Person>() {
new Person() { First = "Jesse", Last = "Gavin", Twitter = "jessegavin" },
new Person() { First = "John", Last = "Sheehan", Twitter = "johnsheehan" }
};
// I want to transform the list above into a JSON object like so
{
columns : ["First", "Last", "Twitter"],
rows: [
["Jesse", "Gavin", "jessegavin"],
["John", "Sheehan", "johnsheehan"]
]
}
Example #2: List<Address> to JSON
// C# list of Locations
var list = new List<Location>() {
new Location() { City = "Los Angeles", State = "CA", Zip = "90210" },
new Location() { City = "Saint Paul", State = "MN", Zip = "55101" },
};
// I want to transform the list above into a JSON object like so
{
columns : ["City", "State", "Zip"],
rows: [
["Los Angeles", "CA", "90210"],
["Saint Paul", "MN", "55101"]
]
}
Is there a way to tell JSON.net to serialize an object in this manner? If not, how could I accomplish this? Thanks.
UPDATE:
Thanks to #Hightechrider's answer, I was able to write some code that solves the problem.
You can view a working example here https://gist.github.com/1153155
Using reflection you can get a list of properties for the type:
var props = typeof(Person).GetProperties();
Given an instance of a Person p you can get an enumeration of the property values thus:
props.Select(prop => prop.GetValue(p, null))
Wrap those up in a generic method, add your favorite Json serialization and you have the format you want.
Assuming your using .Net 4 this should do everything you want. The class actually lets you convert to either XML or JSON. The Enum for CommunicationType is at the bottom. The serializer works best if the class your passing it has been decorated with DataContract & DataMember attributes. I've included a sample at the bottom. It will also take an anonymous type so long as it's all simple types.
Reflection would work as well but then you have to understand all the JSON nuances to output complex data types, etc. This used the built-in JSON serializer in .Net 4. One more note, because JSON does not define a date type .Net puts dates in a funky ASP.Net custom format. So long as your deserializing using the built-in deserializer it works just fine. I can dig up the documentation on that if you need.
using System;
using System.Xml.Serialization;
using System.Text;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Xml.Linq;
internal class Converter
{
public static string Convert<T>(T obj, CommunicationType format, bool indent = false, bool includetype = false)
{
if (format == CommunicationType.XML)
{
return ToXML<T>(obj, includetype, indent);
}
else if (format == CommunicationType.JSON)
{
return ToJSON<T>(obj);
}
else
{
return string.Empty;
}
}
private static string ToXML<T>(T obj, bool includetype, bool indent = false)
{
if (includetype)
{
XElement xml = XMLConverter.ToXml(obj, null, includetype);
if(indent) {
return xml.ToString();
}
else
{
return xml.ToString(SaveOptions.DisableFormatting);
}
}
else
{
System.Xml.Serialization.XmlSerializerNamespaces ns = new System.Xml.Serialization.XmlSerializerNamespaces();
XmlSerializer xs = new XmlSerializer(typeof(T));
StringBuilder sbuilder = new StringBuilder();
var xmlws = new System.Xml.XmlWriterSettings() { OmitXmlDeclaration = true, Indent = indent };
ns.Add(string.Empty, string.Empty);
using (var writer = System.Xml.XmlWriter.Create(sbuilder, xmlws))
{
xs.Serialize(writer, obj, ns);
}
string result = sbuilder.ToString();
ns = null;
xs = null;
sbuilder = null;
xmlws = null;
return result;
}
}
private static string ToJSON<T>(T obj)
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
using (MemoryStream ms = new MemoryStream())
{
string result = string.Empty;
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
ser.WriteObject(ms, obj);
result = encoding.GetString(ms.ToArray());
ms.Close();
encoding = null;
ser = null;
return result;
}
}
}
[DataContract()]
public enum CommunicationType : int
{
[XmlEnum("0"), EnumMember(Value = "0")]
XML = 0,
[XmlEnum("1"), EnumMember(Value = "1")]
JSON = 1
}
[DataContract(Namespace = "")]
public partial class AppData
{
[DataMember(Name = "ID")]
public string ID { get; set; }
[DataMember(Name = "Key")]
public string Key { get; set; }
[DataMember(Name = "Value")]
public string Value { get; set; }
[DataMember(Name = "ObjectType")]
public string ObjectType { get; set; }
}
Any specific reason why you don't need the standard format?
To actually answer the question:
Since this is something that is outside of JSON syntax I can't think of a way to implement this within the default framework.
One solution would be to leverage attributes decorate the properties you want transported over the wired with a custom attribute and using Reflection cycle through the properties and output their property names as the column headers and then cycle throw the objects and write the values. Generic enough so it could be applied across other objects as well.
public class Location
{
[JsonFooAttribute("City")]
public string city {get;set;}
[JsonFooAttribute("State")]
public string state {get;set;}
}