I need to fetch some values obtained from an web URL.
The JSON is as given:
{"country":{"name":"India","State":"Raj": Count :239}, "Population": 25487}
Now i want to fetch the value of Count and Population using C#.
I have tried using JavaScriptSerializer(); But the problem is that its response time is much much slower.
Please suggest me a way to fetch the values from this JSON string.
Thanks
I personally use
https://github.com/ServiceStack/ServiceStack.Text
It's a very fast JSON serializer/deserializer.
I usually create an extension method to make the code tidier:
public static string ToJson(this object _obj)
{
return JsonSerializer.SerializeToString(_obj);
}
Edit:
A quick way to fetch those values would be to create a class of the data:
public class Country
{
public string name { get; set; }
public string State { get; set; }
public int Count { get; set; }
}
public class CountryInformation
{
public Country Country { get; set; }
public int Population { get; set; }
}
Then, using ServiceStack:
void SomeFunction(string _Json)
{
var FeedResult = _Json.FromJson<CountryInformation>();
}
You can then get the values from FeedResult as such:
FeedResult.Country.name;
One option is to use Json.NET - http://json.codeplex.com/
I normally recommend using typed POCO's like #misterjingo suggested.
However for one-off tasks you can use ServiceStack's Json Serializer to parse it dynamically like:
var json = "{\"country\":{\"name\":\"India\",\"State\":\"Raj\": \"Count\": 239}, \"Population\": 25487}";
var jsonObj = JsonObject.Parse(json);
var country = jsonObj.Object("country");
Console.WriteLine("Country Name: {0}, State: {1}, Count: {2} | Population: {3}",
country["name"], country["State"], country["Count"], jsonObj["Population"]);
//Outputs:
//Country Name: India, State: Raj, Count: 239 | Population: 25487
Note: the JSON spec requires an object property name to be a string which is always double quoted.
ServiceStack's JsonSerializer is also the fastest Json Serializer for .NET much faster than other Json Serializers.
You could also use the newer DataContractJsonSerializer class to work with JSON data.
You can either use DataContractSerialiser (ideally if you create the feed) and if you have a tricky malformed json string use JSON.net as it gives you linq2json to parse through it one node at a time.
Related
I need to deserialize my JSON, which looks like this:
{
"cart.empty.title":"Il tuo carrello \u00e8 vuoto",
"returns.confirmation.status.step1.next.arbitration":"A breve riceverai un\u0027email con le istruzioni su come effettuare il reso.",
"returns.home_pickup.address.form.title":"Dove ritireremo i tuoi prodotti?"
}
I tried to do it like this:
string apiResponse = response.Content.ReadAsStringAsync().Result;
drupalRecords = JsonConvert.DeserializeObject<List<Record>>(apiResponse);
public class Record
{
public string Key { get; set; }
public string Value { get; set; }
}
but I have this error:
deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[TranslationsTools.Domain.Entities.Record]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
Do you know how it should be done?
A couple of problems:
Your Record class in no way matches the JSON you've shown. The property names in your object need to match those found in the JSON. In this case that's a bit tricky due to the dots (.) in the JSON property names, but luckly Newtonsoft provides a workaround for that - by using its JsonProperty attribute to match the C# property to the JSON property, even though the names are different.
You need to deserialise to a single object, not a List.
You need:
public class Record
{
[JsonProperty("cart.empty.title")]
public string CartEmptyTitle { get; set; }
[JsonProperty("returns.confirmation.status.step1.next.arbitration")]
public string ReturnsConfirmationStatusStep1NextArbitration { get; set; }
[JsonProperty("returns.home_pickup.address.form.title")]
public string ReturnsHomePickupAddressFormTitle { get; set; }
}
and
var drupalRecord = JsonConvert.DeserializeObject<Record>(apiResponse);
Live demo: https://dotnetfiddle.net/LrNc04
(The creation of the Record class above was the result of pasting your JSON into https://json2csharp.com/ for automatic conversion.)
Your json string defines a dictionary, not a list. If you don't want to define a class, you can simply deserialize the string into a dictionary.
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
public class Program
{
public static void Main()
{
string jsontext = "{ \"cart.empty.title\":\"Il tuo carrello \u00e8 vuoto\", \"returns.confirmation.status.step1.next.arbitration\":\"A breve riceverai un\u0027email con le istruzioni su come effettuare il reso.\", \"returns.home_pickup.address.form.title\":\"Dove ritireremo i tuoi prodotti?\" }";
var record = JsonConvert.DeserializeObject<Dictionary<string, string>>(jsontext);
foreach (var kvp in record) {
Console.WriteLine("{0} = {1}", kvp.Key, kvp.Value);
}
}
}
Demo here
You can access elements of the dictionary in the usual way:
Console.WriteLine(record["cart.empty.title"]); // prints Il tuo carrello รจ vuoto
However, I strongly recommend defining a class and using it like #ADyson suggests in their answer
I have a web service which return response in JSON format as below.
{"123":{"Name":"Abcd", "Age":"30"},"231":{"Name":"xyz", "Age":"20"}, "543":{"Name":"pqr", "Age":"35"}}
I want to deserialize this response in C# and wants to display it.
How can I do with Newtonsoft.Json library.
Please help me.
I'm going to assume that "123", "231", and "543" are identifiers and not constant property names. In that case what you have is a dictionary of objects. First, define a class that maps to the object.
public class Something
{
public string Name { get; set; }
public string Age { get; set; }
}
Then deserialize into a dictionary of those objects.
var whatever = JsonConvert.DeserializeObject<Dictionary<string, Something>>(json);
I have a below code that used JSON.stringify to the object then passed it on POST method (Please see below Javascript code). I'm getting those values on the backend using C#. My problem is, how could I convert/manipulate/access the stringified values. Please see below C# code
Javascript:
var json_db = JSON.stringify(selectedDbInfo);
$.post("../FormActions/DatabaseChanges.aspx", { action: "savedb", orderNumber: orderNumber, selectedDb: json_db},
function (response) {
alert('ok');
});
C#:
var dbValue = c.Request.Params["selectedDb"];
below is the result value of dbValue
"[{\"dbname\":\"BASINS\",\"distance\":\"0\"},{\"dbname\":\"BROWNFIELD\",\"distance\":\"0.5\"},{\"dbname\":\"BRS\",\"distance\":\"0\"}]"
You need to parse the JSON into a .NET array or List.
Many use json.NET for this: http://james.newtonking.com/json
At a push you could use some string manipulation to populate your objects one by one, but I wouldn't recommend that.
There are many samples here on SO.
if you're just want to convert it dictionary look here:
How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?
However, there's a built in mechanism in ASP.NET MVC that serializes automatically your json param to predefined objects at your convenient.
You may define a class having the fields like dbname and distance as properties. Then you may deserialize the json string dbValue into a list of that type using NewtonSoft.Json. Please see the code below:
var list = JsonConvert.DeserializeObject<List<RootObject>>(dbValue);
foreach (var item in list)
{
Console.WriteLine(string.Format("dbname: {0}, distance: {1}", item.dbname, item.distance));
}
Ans the definition of RootObject is as simple as you guess:
public class RootObject
{
public string dbname { get; set; }
public string distance { get; set; }
}
Create a custom serializable data contract class, say DatabaseDistance, with following properties:
[DataMember(Name = "dbname")]
private string name;
[DataMember(Name = "distance")]
private double distance;
and use following method for deserialization:
public static T FromJSON<T>(string jsonValue, IEnumerable<Type> knownTypes)
{
//validate input parameters here
T result = default(T);
try
{
using (MemoryStream stream = new MemoryStream(Encoding.Unicode.GetBytes(jsonValue)))
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T), knownTypes);
result = (T)serializer.ReadObject(stream);
}
}
catch (Exception exception)
{
throw new Exception("An error occurred while deserializing", exception);
}
return result;
}
pass list of your objects as type parameter
Im using NewtonSoft linq 2 json to serialize objects from classes straight to a json string
The class object I'm using is very simple :
public class OverviewQuery
{
public string id { get; set; }
public string method { get; set; }
public string Params { get; set; }
public OverviewQuery(string sid, string smethod, string sparam)
{
this.id = sid;
this.method = smethod;
this.Params = sparam;
}
}
If I serialise this, I get the Json string :
"{\"id\":\"1\",\"method\":\"getStockItemDetails\",\"Params\":\"0000000002\"}"
The Oracle server I'm connecting to (through WebAPI's) requires me to use very very specific naming,
here it should be
"{\"id\":\"1\",\"method\":\"getStockItemDetails\",\"Params\":[\"0000000002\"]}"
Is there any way NewtonSoft implemented a way to achieve this formatting ?
Without correct formatting, the only way to send the information is through hardcoding everything..
What the serialiser is doing with your class seems straightforward.
Generally JSON-RPC services will require that the params value in the envelope be a JSON Array (for indexed parameters) or an Object (for named parameters).
Could you just change your class such that Params is an Array of String ?
The web service I consume responces with json data.
it gives resultObject as array:
resultObject:[{object1}, {object2},...] if there more then one object
and it returns
resultObject:{object1} if there only one object.
for serializing in .NET I created a "static" structure of classes to map json schema. But if in one case i've got an array (list) of objects an in other case just one object, how is it possible to handle this situation?
I have found a plethora of ugly solutions to this one, but so far goes:
If you use the System.Web.Script.Serialization.JavaScriptSerializer you have very limited control. If the result data type is simple, you could simply use the DeserializeObject method; it will translate everything into Dictionary and the "resultObject" property will in the first case be a Dictionary while the latter case will turn it into an array of such dictionary. It will not save you the headache of the final translation, but you will get the data into dictionaries which could be considered a first step.
I also attempted to use the KnownTypes and the DataContractJsonSerializer, but alas the datacontract serializer needs "hints" in the form of specially named properties to aid it deserializing. (Why am I using the KnownType attribute wrong?). This is a hopeless strategy if you don't have any control of the serialization which I guess is the case for you.
So now we are down to the butt-ugly solutions of which trial-and-error is my first choice:
When using the ScriptSerializer the conversion will fail with an InvalidOperationException if anything is wrong. I then created two data types one with data-as-arrays and one where data is a single instance (the DataClass is my invention since you do not specify the data types):
[DataContract]
public class DataClass
{
[DataMember]
public string FirstName { get; set; }
[DataMember]
public int BirthYear { get; set; }
public override string ToString()
{
return "FirstName : '" + FirstName + "', BirthYear: " + BirthYear;
}
}
[DataContract]
public class ResultSingle
{
[DataMember]
public DataClass Data { get; set; }
}
[DataContract]
public class ResultArray
{
[DataMember]
public List<DataClass> Data { get; set; }
}
Using these data types it is possible to translate using
JavaScriptSerializer jSer = new JavaScriptSerializer();
var one = jSer.Deserialize<ResultSingle>(jsonWithSingleInstance);
var many = jSer.Deserialize<ResultArray>(jsonWithArray);
But this of course require you to known the data type in advance and you get two different result types. Instead you could choose to always convert to ResultArray. If you get an exception you should convert as ResultSingle and then instantiate the ResultArray and manually add the data object to the Data list:
static ResultArray ConvertJson(string json)
{
ResultArray fromJson;
JavaScriptSerializer jSer = new JavaScriptSerializer();
try
{
fromJson = jSer.Deserialize<ResultArray>(json);
return fromJson;
}
catch (InvalidOperationException)
{
var single = jSer.Deserialize<ResultSingle> (json);
fromJson = new ResultArray();
fromJson.Data = new List<DataClass>();
fromJson.Data.Add(single.Data);
return fromJson;
}
}
static void Main(string[] args)
{
var jsonWithSingleInstance = "{\"Data\":{\"FirstName\":\"Knud\",\"BirthYear\":1928}}";
var jsonWithArray = "{\"Data\":[{\"FirstName\":\"Knud\",\"BirthYear\":1928},{\"FirstName\":\"Svend\",\"BirthYear\":1930}]}";
var single = ConvertJson(jsonWithSingleInstance);
var array = ConvertJson(jsonWithArray);
}
I do not say this is a beautiful solution (it isn't), but it should do the job.
You could also look at json.net which seem to be the json serializer of choice in .NET: How to install JSON.NET using NuGet?
Well, my service provider finally said that it is really a bug.
http://jira.codehaus.org/browse/JETTISON-102
says that is it because of java version that they use.