Help to deal with JSON deserialization correct answer. For example, we have JSON response to the following:
{"variant":"otvet1",
"source":"otvet2",
"items":[
{"list":"512"},
{"vist":"315"},
{"zist":"561"}]}
To deserialize using the following code:
[DataContract]
public partial class ItemsList
{
[DataMember(Name = "list")]
public string lisType { get; set; }
[DataMember(Name = "vist")]
public string vistType { get; set; }
[DataMember(Name = "zist")]
public string zistType { get; set; }
}
[DataContract]
public partial class SourceList
{
[DataMember(Name = "variant")]
public string variantType { get; set; }
[DataMember(Name = "source")]
public string vistType { get; set; }
[DataMember(Name = "items")]
public List <ItemsList> TestItemsList { get; set; }
}
public class JsonStringSerializer
{
public static T Deserialize<T>(string strData) where T : class
{
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(strData));
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
T tRet = (T)ser.ReadObject(ms);
ms.Close();
return (tRet);
}
}
private static SourceList SourceTempList;
SourceTempList = JsonStringSerializer.Deserialize<SourceList>(e.Result); //in e.Result JSON response
In the previous code, it works, but if you change the JSON response, it does not work ...
New JSON response:
{"variant":"otvet1",
"source":"otvet2",
"items":[3,
{"list":"512"},
{"vist":"315"},
{"zist":"561"}]}
In this case, c # code for deserialization does not work ...
Items in the number 3 appeared, tell me how to deserialize the JSON response to this?
Were available to list vist and zist ...Help me...please
Historically the DataContractJsonSerializer has been seen as broken. The reccomendation is to use JSON.Net http://james.newtonking.com/projects/json-net.aspx
Check out Deserialization problem with DataContractJsonSerializer
Related
So i'm getting a response like this
{"$id":"1","success":true,"errors":{"$id":"2","$values":[]}}
how can i convert this into to a c# object, tried using this(http://json2csharp.com/) tool to make an output but it doesn't make sense
this is what i'm getting
x
public class Errors
{
public string __invalid_name__$id { get; set; }
public List<object> __invalid_name__$values { get; set; }
}
public class RootObject
{
public string __invalid_name__$id { get; set; }
public bool success { get; set; }
public Errors errors { get; set; }
}
I'm kinda new to c#, any inputs would be deeply appreciated, i basically need access to success key variable
You need to add [JsonProperty] attribute to every property that key name started with dollar $
public class Errors
{
[JsonProperty("$id")]
public string id { get; set; }
[JsonProperty("$values")]
public List<object> values { get; set; }
}
public class RootObject
{
[JsonProperty("$id")]
public string id { get; set; }
public bool success { get; set; }
public Errors errors { get; set; }
}
Because the $ indicates metadata, not an actual data field. so you have to modify your JsonSerializerSettings to ignore MetadataPropertyHandling.
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.MetadataPropertyHandling = MetadataPropertyHandling.Ignore;
And finally deserialize your json to above class objects.
RootObject rootObject = JsonConvert.DeserializeObject<RootObject>(json, settings);
Here I created a sample console app for demonstration purpose that shows how above code will work.
class program
{
public static void Main()
{
string json = File.ReadAllText(#"Path to your json file");
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.MetadataPropertyHandling = MetadataPropertyHandling.Ignore;
RootObject rootObject = JsonConvert.DeserializeObject<RootObject>(json, settings);
Console.WriteLine("id: " + rootObject.id);
Console.WriteLine("success: " + rootObject.success);
Console.WriteLine("errors.id: " + rootObject.errors.id);
Console.WriteLine("errors.values: " + string.Join(",", rootObject.errors.values));
Console.ReadLine();
}
}
Output:
Well, What you can do is
public class Errors
{
[JsonProperty(PropertyName = "$id")]
public string id { get; set; }
[JsonProperty(PropertyName = "$values")]
public List<object> values { get; set; }
}
public class RootObject
{
[JsonProperty(PropertyName = "$id")]
public string id { get; set; }
public bool success { get; set; }
public Errors errors { get; set; }
}
You need your object attributes to match you json string ($id instead of _invalid_name_$id), then you can use:
JsonConvert.DeserializeObject<RootObject>(jsonString);
Here is a simple class to serialize json string from object or to object (T). May de/serialize array(list) of objects.
public class HelperSerializer<T> where T: class
{
public static string WriteFromObject(T source)
{
using (var ms = new MemoryStream()) {
var ser = new DataContractJsonSerializer(typeof(T));
ser.WriteObject(ms, source);
byte[] json = ms.ToArray();
return Encoding.UTF8.GetString(json, 0, json.Length);
}
}
// Deserialize a JSON stream to an object.
public static T ReadToObject(string json)
{
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(json)))
{
var ser = new DataContractJsonSerializer(typeof(T));
return ser.ReadObject(ms) as T;
}
}
}
Use persons = HelperSerializer<List<Person>>.ReadToObject(json);
and var json = HelperSerializer<List<Person>>.WriteFromObject(persons);
I have an AJAX call that sends the following JSON to my webservice:
{"Tags":["12","5","2"],"TargetId":"36946","TargetType":"Officer"}
Here's the webservice and custom class:
[WebMethod]
public string UpdateTags(string PostParameters)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
var tagData = serializer.Deserialize<TagData>(PostParameters);
// do some stuff with tagData
}
[Serializable]
[DataContract(Name = "PostParameters")]
public class TagData
{
[DataMember(Name = "TargetId")]
string TargetId { get; set; }
[DataMember(Name = "TargetType")]
string TargetType { get; set; }
[DataMember(Name = "Tags")]
List<int> Tags { get; set; }
}
Unfortunately, the properties of tagData are null. What am I not doing correctly?
Properties in TagData are not public. Make them public and it should work.
Also I would suggest using JSON.NET. It is faster.
I'm using Asp.Net, and although I've done some work deseriaizing Xml before, I've not got to do the same with json.
The error I'm getting is Data at root level is invalid, something I've seen before with Xml deserialization.
Here's the documentation I have for the response:
HTTP ResponseCode: 200
HTTP Status: OK
HTTP Body:
{
“Status”:”Success”,
“Response”:”0”,
“Price”:”10.00”,
“BuyerId”:999,
“BuyerContractId”:9999,
“Detail”:”https://...”
}
I'm using a WebClient to get the data back:
response = wc.UploadString(info.Endpoint, info.Data);
"response" is a string. I use this method to deserialize:
public static T JsonResponse<T>(string response)
where T : class
{
var s = new DataContractJsonSerializer(typeof(T));
using (var r = XmlReader.Create(new StringReader(response)))
{
return (T)s.ReadObject(r);
}
}
The class I'm trying to deserialize to is:
[DataContract]
public class ResponseProps
{
[DataMember(Name = "Status")]
public string Status { get; set; }
[DataMember(Name = "Response")]
public string Response { get; set; }
[DataMember(Name = "Price")]
public decimal Price { get; set; }
[DataMember(Name = "BuyerId")]
public string BuyerId { get; set; }
[DataMember(Name = "BuyerContractId")]
public string BuyerContractId { get; set; }
[DataMember(Name = "Detail")]
public string Detail { get; set; }
}
Here's how it's called:
var cr = XmlHelper.JsonResponse<ResponseProps>(response);
Anyone got any clues as to where I'm going wrong?
Assuming the data comes in JSON format, I changed the following -
public static T JsonResponse<T>(string response)
where T : class
{
return JsonConvert.DeserializeObject<T>(response);
}
Now this works fine-
var q = JsonResponse<ResponseProps>('jsonString');
Hi I have a response from the web service and i would like to have a single url from the response.
My response is in the below format.
[{"cdn_streaming_uri": "9e849cfbb2e157-22558a0600b387d0abe240fe5.r73.stream..rackcdn.com", "name": "test1", "cdn_ios_uri": "d3d4c27-22558a0600b387d0abc071d0ae240kcdn.com", "cdn_ssl_uri": "https://990fea26e-22558a0600b387d0abc071d0ae240fe5.ssl.cdn.com", "cdn_enabled": false, "ttl": 259200, "log_retention": false, "cdn_uri": "99b56a009-22558a0600b3c071d0ae240fe5.r73.n.com"}, {"cdn_streaming_uri": "74ec8c-d5edc6cad91792413b1b134fde.r46.stcdn.com", "name": "test2", "cdn_ios_uri": "d05437e44-d5edc61792413b1b134fde.iosr.cdn.com", "cdn_ssl_uri": "https://a1c2ebbf5-d5edc6cd91792413b1b134fde.scdn.com", "cdn_enabled": false, "ttl": 259200, "log_retention": false, "cdn_uri": "72ffd-d5edc6ca16852413b1b134fde.cdn.com"}, {"cdn_streaming_uri": "93665b76-550971032c2a22cdn.com", "name": "test3", "cdn_ios_uri": "ca6b-550971032c2fbf19452d6a.iosr.cf2.rackcdn.com", "cdn_ssl_uri": "https://c7c39-550971032cbf19452d6cdn.com", "cdn_enabled": true, "ttl": 86400, "log_retention": true, "cdn_uri": "68fc6d831a94-550971032c252d6a.r3cdn.com"}]
I need to the "cdn_streaming_uri" for the name "test3".
You can view the JSON parser in http://json.parser.online.fr/
How do i parse it?
Here is my code:
public static object getTokenResponse(String PrivateURL, string ResponseType)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(PrivateURL +"?format=JSON");
request.Method = "GET";
request.Headers.Add("X-Auth-Token", id);
//request.ContentType = "application/XML";
HttpWebResponse resp1;
try
{
resp1 = (HttpWebResponse)request.GetResponse();
}
catch (Exception exp)
{
string[] st = new string[0];
return st;
}
StreamReader reader = new StreamReader(resp1.GetResponseStream());
string secondresponse = reader.ReadToEnd();
Console.WriteLine(secondresponse);
reader.Close();
JavaScriptSerializer json_serializer = new JavaScriptSerializer();
object obj1 = json_serializer.DeserializeObject(secondresponse);
}
I could see the response in obj1.
The best approach, I think, is to create class which will represent your response. The easiest way is to use Visual Studio's EDIT -> Paste Special -> Paste JSON As Classes option:
You just copy your response and paste it as JSON classes. Visual studio will generate model for you. In this particular case the result will be:
namespace ConsoleApplication91
{
public class Rootobject
{
public Class1[] Property1 { get; set; }
}
public class Class1
{
public string cdn_streaming_uri { get; set; }
public string name { get; set; }
public string cdn_ios_uri { get; set; }
public string cdn_ssl_uri { get; set; }
public bool cdn_enabled { get; set; }
public int ttl { get; set; }
public bool log_retention { get; set; }
public string cdn_uri { get; set; }
}
}
which, of course, does not look very nice, but you're always welcome to refactor this code. When you have your model you download your response, parse it and get what you need using linq, for example:
using (var client = new WebClient())
{
var url = "your service url";
var serializer = new JavaScriptSerializer();
// Response in JSON format
var respJson = client.DownloadString(url);
// Deserialized response
var resp = serializer.Deserialize<Rootobject>(respJson);
// Your requested result
var result = resp.Property1.FirstOrDefault(o => o.name == "test3").cdn_streaming_uri;
}
EDITS:
After refactoring (using DataMember attributes and removing redundant model objects) you can have the following model:
[DataContract]
public class Model
{
[DataMember(Name = "cdn_streaming_uri")]
public string CdnStreamingUri { get; set; }
[DataMember(Name = "name")]
public string Name { get; set; }
[DataMember(Name = "cdn_ios_uri")]
public string CdnIosUri { get; set; }
[DataMember(Name = "cdn_ssl_uri")]
public string CdnSslUri { get; set; }
[DataMember(Name = "cdn_enabled")]
public bool CdnEnabled { get; set; }
[DataMember(Name = "ttl")]
public int Ttl { get; set; }
[DataMember(Name = "log_retention")]
public bool LogRetention { get; set; }
[DataMember(Name = "cdn_uri")]
public string CdnUri { get; set; }
}
After some research I've figured out that JavascriptSerializer is a little deprecated and it does not support any kind of DataMemberAttributes. So I would recommend to use DataContractJsonSerializer. It is little messier than JavascriptSerializer but I think it's fine. If you do not care about any code conventions you can peacefully use the firs provided option (with JavaScriptSerializer and no DataMember attributes). And do not forget to update result query:
using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(respJson)))
{
var serializer = new DataContractJsonSerializer(typeof(Model[]));
var resp = serializer.ReadObject(ms) as Model[];
var result = resp.FirstOrDefault(o => o.Name == "test3").CdnStreamingUri;
}
But, also, if you don't want to use linq (which I'm still strongly recommend) you can create some function which will find the CdnStreamingUri you need:
public static class Extensions
{
public static string GetCdnStreamingUriFor(this Model[] input, string name)
{
foreach (var model in input)
{
if (model.Name == name)
return model.CdnStreamingUri;
}
return string.Empty;
}
}
And your result query will look like:
var result = resp.GetCdnStreamingUriFor("test3");
P.S.
Full list of all used namespaces:
using System;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Runtime.Serialization;
I recommend you to generate class for that json, like the following (http://json2csharp.com/ ):
public class RootObject
{
public string cdn_streaming_uri { get; set; }
public string name { get; set; }
public string cdn_ios_uri { get; set; }
public string cdn_ssl_uri { get; set; }
public bool cdn_enabled { get; set; }
public int ttl { get; set; }
public bool log_retention { get; set; }
public string cdn_uri { get; set; }
}
After that you can deserialize strongly typed object http://msdn.microsoft.com/en-us/library/bb355316(v=vs.110).aspx
i would like use uknown object from JS like this
{"aa":{"abcd
1":{"uio":[],"uio2":[],"uio3":["opee1","opee2","opee3"]},"abcd
2":null,"abcd 3":null,"abcd 4":null}}
sent into MVC with contentType 'application/json'. This object has no permament keys, like this name "abcd 1" can be another name in future.
i have function Test(Object aa) and question is, what type i must use for unknown array of objects or strings. Thanks
Have you tried this website before: http://json2csharp.com/ ?
Though the ASP.NET 4.0 built in JSON to C# parsing/serialization works great and in most cases you can just define a simple C# class that models the "signature" of your JSON and get the correct C# representation of your JSON with a call to something like Test(MyCSharpJsonObj aa), you seem to need something more robust. Note with the code you are about to see below, don't think you can overload your functions like so Test(ListData1 aa), Test(ListData2 aa), ASP.NET won't correctly call the right function for you on request from client and may not even compile correctly, if using Web API, not sure.
You more likely will have to call the right function from the client who is intimate with the JSON being sent in the request like so: Test1(ListData1 aa), Test2(ListData2 aa) using the code like below to assist you:
[Serializable]
[DataContract]
public class ListData1
{
[DataMember]
public string r0 { get; set; }
[DataMember]
public string r1 { get; set; }
[DataMember]
public string r2 { get; set; }
[DataMember]
public List<Data1> optdata { get; set; }
public ListData1() { }
public string ToJson()
{
return JSONHelper.Serialize<ListData1>(this);
}
}
[Serializable]
[DataContract]
public class Data1
{
[DataMember]
public string label { get; set; }
[DataMember]
public string d0 { get; set; }
[DataMember]
public string d1 { get; set; }
public Data1() { }
public string ToJSON()
{
return JSONHelper.Serialize<Data1>(this);
}
}
[Serializable]
[DataContract]
public class ListData2
{
[DataMember]
public string r0 { get; set; }
[DataMember]
public string r1 { get; set; }
[DataMember]
public string r2 { get; set; }
[DataMember]
public List<Data2> optdata { get; set; }
public ListData2() { }
public string ToJson()
{
return JSONHelper.Serialize<ListData2>(this);
}
}
[Serializable]
[DataContract]
public class Data2
{
[DataMember]
public string label { get; set; }
[DataMember]
public string d0 { get; set; }
[DataMember]
public string d1 { get; set; }
[DataMember]
public string d2 { get; set; }
public Data2() { }
public string ToJSON()
{
return JSONHelper.Serialize<Data2>(this);
}
}
public static class JSONHelper
{
public static string Serialize<T>(T obj)
{
System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(obj.GetType());
MemoryStream ms = new MemoryStream();
serializer.WriteObject(ms, obj);
string retVal = Encoding.UTF8.GetString(ms.ToArray());
return retVal;
}
public static T Deserialize<T>(string json)
{
if (string.IsNullOrEmpty(json))
{
return default(T);
}
T obj = Activator.CreateInstance<T>();
MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json));
System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(obj.GetType());
obj = (T)serializer.ReadObject(ms);
ms.Close();
return obj;
}
}
For my personal experience, the most useful way to consume Json inside a code is to use dynamics. Basically, instead of serializing/deserializing to specific type, you convert json object to dynamic one.
In this case you loose compile time validation, but get ability to support of any document structure.
In MVC4 you may use build-in Json.Decode() method from System.Web.Helpers.dll which will return you dynamic object. Otherwise, there are lots of libraries for that specific purpose, like Json.Net
Finally I found solution. Use Newtonsoft.Json and this code as sample for dynamic data structure:
$.ajax({
...
data: {data: JSON.stringify({first: "name", next: ["a", "b"], last: {o: "in"}})}
})
[HttpPost]
public JsonResult SaveMenu(String data)
{
dynamic JData = JObject.Parse(data);
//--now JData.first == "name"
if (JData.last is JObject)
{
}
//--or dynamic foreach
foreach (dynamic own in JData)
{
//--own.Name == first, next and last
//--access to variable == JData[own.Name]
if (JData[own.Name] is JArray)
{
foreach (String var in JData[own.Name])
{
}
}
}
}