i want to convert Dictionary into Json. the key is a string and the value is a ICollection of CompaignModel :
IDictionary<string, ICollection<BuzzCompaignModel>> BuzzCompaignByInterest ;
and here is what contain BuzzCompaign Model :
public class BuzzCompaignModel
{
public long BuzzCompaignId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public DateTime EndDate { get; set; }
}
how can i convert BuzzCompaignByInterest into JSON
Use Json.NET and JsonConvert
http://james.newtonking.com/projects/json/help/SerializingJSON.html
It doesn't matter dictionary or object are serialising to Json
Related
Hello i want to deserialize a class which contains a string, a bool and a List<[mystructtype>;When using JsonConvert.Deserialize<[myclass]> it deserializes the string and the bool correctly but not the List<[Struct]>.I have also changed the List<struct> with an array of structs.Both the class container and the struct are marked with Serializeable and i do not understand where the problem is.
Can anyone help me?
Struct
[Serializable]
public struct Status
{
//identifiers
public long playerId { get; set; }
public long groupId { get; set; }
public int type { get; set; }
}
Class Container
[Serializable]
class GatewayDeviceResponse
{
public bool status { get; set; } //gets deserialized good
public string message { get; set; } //gets deserialized good
public Status[] data { get; set; } // all members are defaults
}
Deserialization
IRestResponse response = client.Execute(request);
string result = Encoding.UTF8.GetString(response.RawBytes);
GatewayDeviceResponse resp = JsonConvert.DeserializeObject<GatewayDeviceResponse>(result);
return resp.data.ToList();
P.S The string is a response from a webserver,and i am using RestSharp for creating the server request and getting the response.The thing is the response string is good.The class is deserialized good excluding the collection.
What could the problem be?
P.S
The string response from the server i get is :
"{
\"status\":true,
\"message\":\"ok\",
\"data\":[
{
\"status\":{
\"playerId\":59,
\"groupId\":26,
\"type\":2,
\"deviceId\":\"abababa",
\"groupName\":\"srl\",
\"playerName\":\"Adrian\"
}
},
{
\"status\":{
\"playerId\":25,
\"groupId\":26,
\"type\":1,
\"deviceId\":\"lalal\",
\"groupName\":\"srl\",
\"playerName\":\"Alex\"
}
}
]
}"
The Status[] array elements are not supposed to be fully filled by the server response , just the 3 fields i have posted in the POCO/
I wrote a unit test as below and it passes and correctly deserialized with restsharp. Can you replace your response string and classes to check unit test still pass?
May be your class representation is not fit for your response. Take a little help from http://json2csharp.com/ and check your classes represents your json correctly.
[Test]
public void Can_Deserialize_Struct()
{
var data = "{ \"myList\":[{\"name\": \"test1234\"}] }";
JsonDeserializer json = new JsonDeserializer();
var output = json.Deserialize<MyTest>(new RestResponse { Content = data });
Assert.NotNull(output);
}
class MyTest
{
public List<MyStruct> MyList { get; set; }
}
struct MyStruct
{
public String name { get; set; }
}
According to your response string, your c# classes should represent your json as below :
public class Status
{
public int playerId { get; set; }
public int groupId { get; set; }
public int type { get; set; }
public string deviceId { get; set; }
public string groupName { get; set; }
public string playerName { get; set; }
}
public class StatusData
{
public Status status { get; set; }
}
public class GatewayDeviceResponse
{
public bool status { get; set; }
public string message { get; set; }
public List<StatusData> data { get; set; }
}
It does not related with type struct or class. You need to add another class in front of your status representation class. Because it starts as a json object in your response string.
I'm quite new to JSON and would like to ask your help.
I'm trying to deserialize Json into a object class but whenever I call JavaScriptSerializer.Deserialize I get this error:
Error: 0x1 at Data Flow Task, Error Getting Data From Webservice!:
System.MissingMethodException: No parameterless constructor defined
for type of 'CurrencyOut[]'. at
System.Web.Script.Serialization.ObjectConverter.ConvertDictionaryToObject(IDictionary`2
dictionary, Type type, JavaScriptSerializer serializer, Boolean
throwOnError, Object& convertedObject)
Here's the Json source:
{"terms":"http://www.xe.com/privacy.php","privacy":"http://www.xe.com/legal/dfs.php","to":"IDR","amount":100.0,"timestamp":"2015-06-01T16:00:00Z","from":[{"quotecurrency":"USD","mid":0.0075713171}]}
Here's my class:
class CurrencyOut
{
public string Tems { get; set; }
public string Privacy { get; set; }
public CurrArr From { get; set; }
public decimal Amount { get; set; }
public DateTime Timestamp { get; set; }
public string To { get; set; }
}
class CurrArr
{
public string QuoteCurrency { get; set; }
public decimal Mid { get; set; }
}
Thanks
I have the following class Visual Studio created from the JSON pasted below.
public class Rootobject
{
public Class1[] Property1 { get; set; }
}
public class Class1
{
public string id { get; set; }
public string comments { get; set; }
public DateTime createdDate { get; set; }
public DateTime modifiedDate { get; set; }
public string createdBy { get; set; }
public string modifiedBy { get; set; }
}
-----JSON-----
[{"id":"00a17000000LmTOAA0","comments":"This is a comment or note added from code","createdDate":"2015-03-13T15:52:02.000+0000","modifiedDate":"2015-03-13T15:52:02.000+0000","createdBy":"Contact","modifiedBy":"Contact"},{"id":"00a17000000LmTTAA0","comments":"This is a comment or note added from code","createdDate":"2015-03-13T15:53:19.000+0000","modifiedDate":"2015-03-13T15:53:19.000+0000","createdBy":"Contact","modifiedBy":"Contact"},{"id":"00a17000000LmTYAA0","comments":"This is a comment or note added from code","createdDate":"2015-03-13T15:54:29.000+0000","modifiedDate":"2015-03-13T15:54:29.000+0000","createdBy":"Contact","modifiedBy":"Contact"},{"id":"00a17000000LmU7AAK","comments":"New Note Entered by Requester: This is a comment or note added from code","createdDate":"2015-03-13T16:39:43.000+0000","modifiedDate":"2015-03-13T16:39:43.000+0000","createdBy":"Contact","modifiedBy":"Contact"},{"id":"00a17000000LmW3AAK","comments":"added this comment from SalesF app as an agent","createdDate":"2015-03-13T17:37:24.000+0000","modifiedDate":"2015-03-13T17:37:24.000+0000","createdBy":"Agent","modifiedBy":"Agent"}]
I'm trying to create an object and do a foreach and get the data... I keep getting error Cannot deserialize the JSON array (e.g. [1,2,3]) into type ' ' because type requires JSON object (e.g. {“name”:“value”}) to deserialize correctly.
I have tried this among other things:
var jsonResponseNotes = JsonConvert.DeserializeObject<Rootobject>(GetCommentsByID());
Any Ideas would be greatly appreciated!!!
you just need the root class:
public class RootObject
{
public string id { get; set; }
public string comments { get; set; }
public string createdDate { get; set; }
public string modifiedDate { get; set; }
public string createdBy { get; set; }
public string modifiedBy { get; set; }
}
and then deserialize it like this:
var result = JsonConvert.DeserializeObject<List<RootObject>>(json);
This is a list of your object. You will need to deserialize as such. Your code should be:
var jsonResponseNotes = JsonConvert.DeserializeObject<List<Class1>>(GetCommentsByID());
Edit
Just noticed your root object property is an array. Still, I believe arrays deserialize into list objects. I've adjusted my code.
I have a simple json string which contains a collection of objects
http://sandapps.com/InAppAds/ads.json.txt
When I call GetAsync to get the objects, the collection returns 1 element instead of 4 and it's empty:
new JsonServiceClient ().GetAsync<List<CrossSell>> (url, Success, Failure);
My class is simple:
class CrossSell
{
public string ID { get; set; }
public string AppCategory { get; set; }
public string AppID { get; set; }
public string Name { get; set; }
public string ImageUrl { get; set; }
public string Copy { get; set; }
public string Device { get; set; }
public string Link { get; set; }
}
The JSON assumes a response DTO like:
class CrossSellResponse {
List<CrossSell> CrossSells { get; set; }
}
new JsonServiceClient().GetAsync<CrossSellResponse> (url, Success, Failure);
and not a bare array as your C# example suggests:
new JsonServiceClient ().GetAsync<List<CrossSell>> (url, Success, Failure);
I found the answer. You need to create an overall wrapper class that holds the JSON response. I call mine CrossSellResponse
public class CrossSellResponse
{
public List< CrossSell> CrossSells {get; set;}
}
The class CrossSell defines the data in the collection and matches the field names in a case sensitive way. The name of the response payload property CrossSells matches the name of the collection in the json stream.
I am having some issues DeSerializing the following Json, it runs, throws no errors however the props in the class are all null so it is obviously not working.
{ "realms":[ { "type":"pve", "queue":false, "status":true, "population":"medium", "name":"Malfurion", "slug":"malfurion" } ] }
The above JSON is the jsonResult string so string jsonResult = the above JSON
My code:
public class Realm
{
public string type { get; set; }
public bool queue { get; set; }
public bool status { get; set; }
public string population { get; set; }
public string name { get; set; }
public string slug { get; set; }
}
var realm = javaScriptSerializer.Deserialize<Realm>(jsonResult);
There are no props in the object because the object contains a single array with a single object in the array.