What you have shown in your question is invalid JSON. I guess you meant this:
{
"ea:productionId": "123",
....
}
which is pretty easy to achieve with Json.NET
serializer using [DataContract]
and [DataMember]
attributes on your model:
[DataContract]
public class JsonClass
{
[DataMember(Name = "ea:productionId")]
public string ProductId { get; set; }
}
and then:
JsonClass result = JsonConvert.DeserializeObject<JsonClass>(jsonresult);
If you don't want to use third party JSON serializers you could use the built-in DataContractJsonSerializer
class which also respects the DataContract and DataMember attributes:
var serializer = new DataContractJsonSerializer(typeof(JsonClass));
byte[] data = Encoding.UTF8.GetBytes(jsonresult);
using (var stream = new MemoryStream(data))
{
var result = (JsonClass)serializer.ReadObject(stream);
}
UPDATE:
It looks like you are using .NET 2.0 and cannot rely on the newer serializers. With the JavaScriptSerializer you have the possibility to write a custom converter:
public class MyJavaScriptConverter : JavaScriptConverter
{
private static readonly Type[] supportedTypes = new[] { typeof(JsonClass) };
public override IEnumerable<Type> SupportedTypes
{
get { return supportedTypes; }
}
public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{
if (type == typeof(JsonClass))
{
var result = new JsonClass();
object productId;
if (dictionary.TryGetValue("ea:productionId", out productId))
{
result.ProductId = serializer.ConvertToType<string>(productId);
}
... so on for the other properties
return result;
}
return null;
}
public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
{
throw new NotImplementedException();
}
}
and then:
var serializer = new JavaScriptSerializer();
serializer.RegisterConverters(new[] { new MyJavaScriptConverter() });
var result = serializer.Deserialize<JsonClass>(jsonresult);
alternatively you could use a weakly typed dictionary instead of the model:
var serializer = new JavaScriptSerializer();
var res = (IDictionary<string, object>)serializer.DeserializeObject(jsonresult);
string productId = res["ea:productionId"] as string;