hello this my json value
How can I get a value ?
"a195cc41-4b37-4192-8949-86ae109cfa80,{\"UserID\":2,\"CurrentSessionID\":\"746076a0-6ab9-415a-849a-2755837981bb\",\"Name\":\"SERDAR\",\"Surname\":\"SENGUL\",\"Email\":null,\"Password\":null,\"RememberMe\":false,\"IsActive\":false,\"IsDeleted\":false,\"CreateDate\":\"2015-01-17T09:15:03.6339968+02:00\"},17.01.2015 09:15:03"
var id = clientAuth.Split(',')[0] > it's working (773484c2-5730-4cfa-a17e-2eb041e8c225)
var date = clientAuth.Split(',')[11] > it's working (24.01.2015)
but I can not take get usermodel a value
that comes
var UserID = clientAuth.Split(',')[1] {\"UserID\":2"
var name = clientAuth.Split(',')[2] {\"Surname\":SENGUL"
var createDate = clientAuth.Split(',')[2] "\"CreateDate\":\"2015-01-17T08:44:30.1681286+02:00\"}"
If I understand your questions, you will need to do this:
var firstComma = rawData.indexOf(',');
var lastComma = rawData.lastIndexOf(',');
var jsonString = rawData.slice(firstComma+1,lastComma-firstComma);
var obj = JSON.parse(jsonString);
and then you can refer to the properties of the object like:
var createdate = obj.CreateDate;
var surname = obj.Surname;
Good Luck.
EDIT: You can also use regular expressions to find the '{..}' string, if you prefer.
1.It's not a valid json.
2.Use Json.net for parsing json's in you C# code:
YourObject obj = JsonConvert.DeserializeObject<YourObject>(json);
Related
I want the JSON array element to be converted as a string.
The JSON looks like this:
[
{
"id":373313181,
"from":"no-reply#email.com",
"subject":"example subject 123",
"date":"2022-01-06 13:22:14"
}
]
I want to get the ID element as a string.
I tried to do like that:
var json = "[{\"id\":373313181,\"from\":\"no-reply#email.com\",\"subject\":\"example subject 123\",\"date\":\"2022-01-06 13:22:14\"}]";
var parse = JObject.Parse(json);
var id = parse["id"].ToString();
Console.WriteLine(id);
So that the output will be like this:
373313181
But that simply just didn't work. Any ideas why?
Parse as JArray and take the first element of the array.
var json = "[{\"id\":373313181,\"from\":\"no-reply#email.com\",\"subject\":\"example subject 123\",\"date\":\"2022-01-06 13:22:14\"}]";
var array = JArray.Parse(json);
var id = (string)array[0]["id"];
And from your question, cast the id to string as below:
var id = (string)array[0]["id"];
OR
var id = array[0]["id"].Value<string>();
Sample Program
My implementation:
using System;
using Newtonsoft.Json;
public class Program
{
public static void Main()
{
var json = "[{\"id\":373313181,\"from\":\"no-reply#email.com\",\"subject\":\"example subject 123\",\"date\":\"2022-01-06 13:22:14\"}]";
dynamic test = JsonConvert.DeserializeObject(json);
Console.WriteLine(Convert.ToString(test[0].id));
Console.WriteLine(Convert.ToString(test[0].id).GetType());
}
}
//Output
373313181
System.String
I have the below dynamic array
dynamic[] queueList = await queueInfo.Run.GetData<dynamic[]>(Name);
I want to get the unique values present in ImportTableName field. So, after some search I found this,
var distNames = queueList.Select(o => new {o.ImportTableName }).Distinct();
which works as expected.
But I don't want to hard code the fieldname "ImportTableName". So tried doing something like this
dynamic[] queueList = await queueInfo.Run.GetData<dynamic[]>(Name);
string tableName = "ImportTableName"
var distNames = queueList.Select(o => new {o.tableName }).Distinct();
But it doesn't work, result is just NULL. Can someone help me to achieve this ?
As it is array of JObject, which has indexer, you can apply it to each element to get value of desired field:
var distNames = queueList.Select(x => x[tableName]).Distinct().ToList();
My solution is:
dynamic[] queueList = new dynamic[]{new { id = 1, ImportTableName = "Data"}};
string propName = "ImportTableName";
var distNames = queueList.Select(d => d.GetType().GetProperty(propName).GetValue(d, null)).Distinct();
It returns as expected "Data".
I am trying to implement a dynamic condition on a JSON Object.
After searching for a while, I am not able to find any resources which can meet my needs.
In the below fashion, I am getting the JsonObject dynamic string into jsonObject variable.
string inputFromAPI = client.GetStringAsync(URL).Result;
dynamic jsonObject = JValue.Parse(inputFromAPI);
I now have a condition which can change on need basis.For example I can have a condition which says
"inputFromAPI.DidWeCharge == true && inputFromAPI.DidWeHold == false"
The above line can change on the fly. Any inputs how to address this will be greatly appreciated.
I can't comment for clarification (don't have the rep yet), but if you need to be able to read the property name because it's changing you can do this. You would have to know the name though.
var JSONobj = JObject.Parse(json);
foreach (JToken child in JSONobj.Children())
{
var prop = child as JProperty;
var propertyName = prop.Name; // this will give you the name of the property
if (propertyName == "DidWeCharge")
{
var value = prop.Value; // Do something here with value?
}
if (propertyName == "DidWeHold")
{
var value = prop.Value; // Do something here with value?
}
var propertyType = prop.Value.Type; // this return the type as a JTokenType enum.
}
I don't know how nested your JSON is, so you may have to traverse further down with another foreach on the child by doing child.Children().
You can use ExpandoObject:
var expandoObj = JsonConvert.DeserializeObject<ExpandoObject>(jsonObject);
expandoObj.yourProperty
JsonConvert is from Newtonsoft.Json package.
You may be able to use Jpath:
using Newtonsoft.Json -
....
var json = #"
{
stuff : [
{
value : 1
},
{
value : 2
}
]
}";
var token = JToken.Parse(json);
var something = token.SelectTokens("$.stuff[?(#.value == 1)]").ToList();
Further on with Newtonsoft.Json, Path returned multiple tokens,
For this code:
JObject o = JObject.Parse(jsStr);
IEnumerable<JToken> selEnum = o.SelectTokens(theFilter);
where the jsStr is the content of https://api.github.com/search/repositories?q=Newtonsoft.Json&sort=stars&order=desc, and theFilter can be any valid JPATH query string (e.g., ".items" or ".items[*].owner").
How to return the selected as a valid json string?
It sounds like you just need Json.SerializeObject:
var o = JObject.Parse(jsStr);
var selEnum = o.SelectTokens(theFilter);
var newJson = JsonConvert.SerializeObject(selEnum);
This will give you JSON representing an array of all of the owner values from the original JSON.
How to deserialize with keyvaluepair the above json
string stations = [{"2":false,"1":"Aforre","0":"WS6"},{"2":false,"1":"Alodtau","0":"WS3"},{"2":false,"1":"Balimo Station","0":"WS36"}]
I what like this
var get = js.Deserialize<Dictionary<string,dynamic>>(stations);
try this:
string stations = "[{'2':false,'1':'Aforre','0':'WS6'},{'2':false,'1':'Alodtau','0':'WS3'},{'2':false,'1':'Balimo Station','0':'WS36'}]";
var serializer = new JavaScriptSerializer();
dynamic value = serializer.DeserializeObject(stations);
and you can access objects like:
var a = value[0]["0"];
and a will have "WS6" (according to your JSON)
The JSON shown is an array. You might try desetializing to:
Dictionary<string, object>[]
i.e.
var get = js.Deserialize<Dictionary<string,object>[]>(stations);
One of the best option is create a class that consist of all keyvalue pair and then deserialize json string to the object of created class
You can try using dynamic variable as following:
string stations = "[{'2':false,'1':'Aforre','0':'WS6'},{'2':false,'1':'Alodtau','0':'WS3'},{'2':false,'1':'Balimo Station','0':'WS36'}]";
var serializer = new JavaScriptSerializer();
dynamic serializevalues = serializer.DeserializeObject(stations);
var valueof1 = serializevalues[0]["1"];
Response.Write(valueof1);
The above will print output "Aforre'.