I have a JSON file like this
{
...
...
...
"ExternalApp": {
"BaseUrl": "https://xxx.xxx.com",
"CallbackUrl": "https://localhost/TestCallBack",
"TokenKey": "xxxx",
"TokenSecret": "xxxx"
}
...
...
...
}
As you can see, the JSON file contains a lot of fields. One of the the setting is ExternalApp. I need to read this ExternalApp into my setting class. In .NET Core, it is very simple by using ConfigurationBuilder. But how to do it in legacy class (non .NET Core).
public class ExternalAppSetting
{
public string BaseUrl;
public string CallbackUrl;
public string TokenKey;
public string TokenSecret;
...
...
...
}
Thanks for any help.
Here is how you can do it using Json.NET:
string json = #"{
""SomeProp1"": ""SomePropValue"",
""ExternalApp"": {
""BaseUrl"": ""https://xxx.xxx.com"",
""CallbackUrl"": ""https://localhost/TestCallBack"",
""TokenKey"": ""xxxx"",
""TokenSecret"": ""xxxx""
},
""SomeProp2"": ""SomeProp2Value"",
}";
JObject o = JObject.Parse(json);
JToken jt = o.SelectToken("ExternalApp");
ExternalAppSetting eas = jt.ToObject<ExternalAppSetting>();
You can do this with JSON.NET.
try this:
ExternalAppSetting externalAppSetting = new ExternalAppSetting();
var token = JToken.Parse(json);
var externalApp = token.SelectToken("ExternalApp");
externalAppSetting.BaseUrl = externalApp["BaseUrl"].ToString();
externalAppSetting.CallbackUrl = externalApp["CallbackUrl"].ToString();
externalAppSetting.TokenKey = externalApp["TokenKey"].ToString();
externalAppSetting.TokenSecret = externalApp["TokenSecret"].ToString();
where json is your json string.
Related
I am really not sure if this could be achievable, How can i remove some nested keys in an Object and make the object very flat. I have a dynamic object as follows,
EventData": { "ChangeSet": { "Change": {
"changes": [
] } } }
and i want to change the above to
EventData": { [] }
is this can be achieved in C#?
Use the NewtonSoft.JSon package.. Following code does the trick. I made it a string array because I do not know what you need but you can change this to your liking.
const string complex = "{\"EventData\": { \"ChangeSet\": { \"Change\": { \"changes\" : [ ]}}}}";
Call to method:
string simple = returnSimpleObject(complex);
public class SerializeData
{
public string[] EventData { get; set; }
}
private static string returnSimpleObject(string Json)
{
JObject jobject = JObject.Parse(Json);
JToken tEventData = jobject.SelectToken("EventData");
SerializeData myEvent = tEventData.ToObject<SerializeData>();
JToken tchanges = jobject.SelectToken("EventData.ChangeSet.Change.changes");
myEvent.EventData = tchanges.ToObject<string[]>();
JsonSerializer serializer = new JsonSerializer();
StringWriter strWrite = new StringWriter();
JsonWriter myWriter = new JsonTextWriter(strWrite);
serializer.Serialize(myWriter, myEvent);
return strWrite.ToString();
}
I have a web api controller in .NET Core 2.1, which receives
JToken jsonBody
The json has the following structure
{
"id": "xxx",
"payload": {
"TelephoneNumber": "1111",
"Name": "Hans"
}
}
and more fields, but it's irrelevant.
I want to retrieve the Number and Name elegantly. Currently, I do the following, which I'm sure could be done in a nicer way:
var payload = JObject.Parse(jsonBody.SelectToken("Payload").ToString());
telephoneNumber = new TelephoneNumber(payload.SelectToken("TelephoneNumber").ToString());
I've tried just doing
jsonBody.SelectToken("Payload.TelephoneNumber")
but that doesn't work. I think that it's because somehow the jsonBody, that the controller receives, has only parsed the top nodes as json, hence it could be that it regards the value of
jsonBody.SelectToken("Payload")
as a string.
As per official documentation - you can do something like this:
var phone = jsonBody["payload"]["TelephoneNumber"].ToString();
var name = jsonBody["payload"]["Name"].ToString();
See a live demo on rextester.
This is at least a little bit more elegant:
var jsonBody = JObject.Parse(#"{
'id': 'xxx',
'payload': {
'TelephoneNumber': '1111',
'Name': 'Hans'
}
}");
var phone = jsonBody["payload"]["TelephoneNumber"].Value<string>();
var name = jsonBody["payload"]["Name"].Value<string>();
If you don't want to deserialize your full json, you can create a class with the properties you need
public class Payload
{
public string TelephoneNumber { get; set; }
public string Name { get; set; }
}
And then use JsonTextReader to deserialize the string:
private static Payload DeserializePayload(JToken token)
{
var serializer = new JsonSerializer();
using (JsonTextReader reader = new JsonTextReader(new StringReader(token.ToString())))
{
reader.CloseInput = true;
while (reader.Read())
{
if (reader.TokenType == JsonToken.StartObject && reader.Path.Equals("payload"))
{
var payload = serializer.Deserialize<Payload>(reader);
return payload;
}
}
}
// not found - return null? throw exception?
return null;
}
Testing the code:
var token = JToken.Parse(#"{
""id"": ""xxx"",
""payload"": {
""TelephoneNumber"": ""1111"",
""Name"": ""Hans""
}
}");
Payload payload = DeserializePayload(token);
Console.WriteLine($"Name: {payload.Name}, Phone number: {payload.TelephoneNumber}");
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();
}
}
I have written these lines of code which make an API request and in return I get valid JSON response:
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(_baseAddress);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.GetAsync(_apiUrl);
if (response.IsSuccessStatusCode)
{
var data = await response.Content.ReadAsAsync<ExpandoObject>();
return Json(data);
}
}
The data looks like below:
Is it possible to get the value of ProductID and Price. I want to assign them to something like:
int productId = ...
int price = ...
How can i do this using C#?
Check out the Newtonsoft Json nuget package. Basically, you create an model with the variables you need, then you call the deserialize method from Newtonsoft. Here's some pseudo code
public class MyObject
{
int ProductID { get; set; }
int Price { get; set; }
int Systems { get; set; }
}
Then in your method:
using Newtonsoft.Json;
public class MyMethod(string json)
{
MyObject obj = JsonConvert.DeserializeObject<MyObject>(json);
}
Something like that.
.Net 4.0 supports creating dynamic objects directly from json:
JavaScriptSerializer serializer = new JavaScriptSerializer();
dynamic item = serializer.Deserialize<object>("{ \"productId\":\"124889\" }");
string test= item["productId"];
If your using Json.NET or Newtonsoft.Json.Linq - this answer should help you.
Json.Net
dynamic stuff = JsonConvert.DeserializeObject("{ 'Name': 'Jon Smith', 'Address': { 'City': 'New York', 'State': 'NY' }, 'Age': 42 }");
string name = stuff.Name;
string address = stuff.Address.City;
Newtonsoft.Json.Linq
dynamic stuff = JObject.Parse("{ 'Name': 'Jon Smith', 'Address': { 'City': 'New York', 'State': 'NY' }, 'Age': 42 }");
string name = stuff.Name;
string address = stuff.Address.City;
string name = stuff.Name;
string address = stuff.Address.City;
I am sorry to answer to my own question but I just found the solution and i wanted to post here:
You need to add these lines of code after data
var _dataResponse = JToken.Parse(JsonConvert.SerializeObject(data));
var _dataResponseProductID = _dataResponse["ProductID"];
var _dataResponsePrice = _dataResponse["Price"];
After that the values taken can be converted to desired data types.
Create an object and Deserialize the json object.
http://www.newtonsoft.com/json/help/html/t_newtonsoft_json_jsonconvert.htm
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/