get data from json array string to an string variable - c#

I get from another app an HTTP request with a JSON payload like this:
{
"reportName": "myfile",
"type_1": "pdf",
"paramm": [
{ "REF": "value1"},
{ "whatevervalue2": "whatevervalue2" }
]
}
I receive the data and try to process it. I created a class to get the data from the JSON:
public class ReportId
{
public string reportName { get; set; }
public string type_1 { get; set; }
public object[] paramm { get; set; }
}
And here is the method where I process all the data on the API controller:
[HttpPost]
public IActionResult generate([FromBody] ReportId jsonResult)
{
try
{
var reportName = jsonResult.reportName;
var type = jsonResult.type_1;
var recParam = jsonResult.paramm;
//....
List<ReportParameter> parameters = new List<ReportParameter>();
foreach (var t in recParam)
{
string[] paramName;
paramName = t.ToString().Split(":");
parameters.Add(new ReportParameter()
{
Name = paramName[0],
Labels = new List<string>() { paramName[1] },
Values = new List<string>() { paramName[1] }
});
}
reportWriter.SetParameters(parameters);
//....
}
catch
{
return null;
}
}
I got no problems with reportName and type_1 but on paramm it gets kinda wonky. I need to get the values of that array from the JSON. I do manage to get them, but with "all the format". For example, after the split on the first one I would get:
"{\r\n \"REF\""
" \"value1\"\r\n}"
Is there any more elegant way to get the values from that paramm array without having to replace the characters that are not part of the "real" string?
Thanks in advance.
[EDIT for clarification]
On the JSON I'll get multiple parameters:
"paramm": [
{ "parameter1": "value1" },
{ "parameter2": "value2" },
{ "parameter3": "value3" }
]
The parameter name could be any word and the value could be any as well. Those parameters are being sent to an RDL to filter some queries, so I need to get the name (parameter1, 2, 3...) and its value and add it as a parameter on the iteration of each.

Yes, there is an easier way.
In your ReportId class, change the paramm member to be a List<Dictionary<string, string>> instead of object[].
public class ReportId
{
...
public List<Dictionary<string, string>> paramm { get; set; }
}
Then, in your generate method you can just get the key and value from each dictionary instead of needing to do string manipulation:
List<ReportParameter> parameters = new List<ReportParameter>();
foreach (var dict in recParam)
{
var kvp = dict.First();
parameters.Add(new ReportParameter()
{
Name = kvp.Key,
Labels = new List<string>() { kvp.Value },
Values = new List<string>() { kvp.Value }
});
}

I guess, Your ReportId class should be like this. I hope this will work.
public partial class ReportId
{
[JsonProperty("reportName")]
public string ReportName { get; set; }
[JsonProperty("type_1")]
public string Type1 { get; set; }
[JsonProperty("paramm")]
public Paramm[] Paramm { get; set; }
}
public partial class Paramm
{
[JsonProperty("REF", NullValueHandling = NullValueHandling.Ignore)]
public string Ref { get; set; }
[JsonProperty("whatevervalue2", NullValueHandling = NullValueHandling.Ignore)]
public string Whatevervalue2 { get; set; }
}

Related

How to find a specific value in an IDictionary<string, object>?

This IDictionary<string, object> contains user data I'm logging into mongodb. The issue is the TValue is a complex object. The TKey is simply the class name.
For example:
public class UserData
{
public string FirstName { get; set; }
public string LastName { get; set; }
public Admin NewAdmin { get; set; }
}
public class Admin
{
public string UserName { get; set; }
public string Password { get; set; }
}
Currently, I'm trying to iterate through the Dictionary and compare types but to no avail. Is there a better way of doing this or am I missing the mark?
var argList = new List<object>();
foreach(KeyValuePair<string, object> kvp in context.ActionArguments)
{
dynamic v = kvp.Value;
//..compare types...
}
Just use OfType<>(). You don't even need the key.
public static void Main()
{
var d = new Dictionary<string,object>
{
{ "string", "Foo" },
{ "int", 123 },
{ "MyComplexType", new MyComplexType { Text = "Bar" } }
};
var s = d.Values.OfType<string>().Single();
var i = d.Values.OfType<int>().Single();
var o = d.Values.OfType<MyComplexType>().Single();
Console.WriteLine(s);
Console.WriteLine(i);
Console.WriteLine(o.Text);
}
Output:
Foo
123
Bar
Link to Fiddle

How to itrate JSON object?

I trying to get the id and email list from the JSON. How can i achieve this?
My JSON string is
{
"name":"name1",
"username":"name1",
"id":505,
"state":"active",
"email":"name1#mail.com",
},
{
"name":"name2",
"username":"name2",
"id":504,
"state":"active",
"email":"name2#mail.com",
}
My code is
Dictionary<string, string> engineers = new Dictionary<string, string>();
using (StreamReader r = new StreamReader(#"D:\project\Gitlap\EngineerEmail\jsonlist5.json"))
{
using (JsonTextReader reader = new JsonTextReader(r))
{
JObject o2 = (JObject)JToken.ReadFrom(reader);
string id = o2["id"].ToString();
string email = o2["email"].ToString();
engineers.Add(email, id);
}
}
class UserItems
{
public string id { get; set; }
public string email { get; set; }
}
I can able to get the first person`s mail ID and ID details. I need to iterate this JSON and get all the mail ID and ID.
I don`t know that how to iterate this JSON. I tried some method from the internet but that was not succeeded.
How can I do?
First thing is your JSON input is not valid json, you need to fix it. There are two issues in it. Its not collection of json objects and comma is missing between two objects.
Valid json should look like below.
[{
"name":"name1",
"username":"name1",
"id":505,
"state":"active",
"email":"name1#mail.com",
},
{
"name":"name2",
"username":"name2",
"id":504,
"state":"active",
"email":"name2#mail.com",
}]
Now define a c# class representing your json object.
public class User
{
public string name { get; set; }
public string username { get; set; }
public int id { get; set; }
public string state { get; set; }
public string email { get; set; }
}
Use JSON.Net library to deserialize it as shown below.
private void button1_Click(object sender, EventArgs e)
{
if(File.Exists("json1.json"))
{
string inputJSON = File.ReadAllText("json1.json");
if(!string.IsNullOrWhiteSpace(inputJSON))
{
var userList = JsonConvert.DeserializeObject<List<User>>(inputJSON);
}
}
}
JObject o2 = (JObject)JToken.ReadFrom(reader);
foreach(var obj in o2)
{
string id = obj["id"].ToString();
string Email= obj["Email"].ToString();
engineers.Add(email, id);
}
I would recommend using the Json.NET NuGet package to accomplish this.
Firstly, create a model to represent your JSON data. Typically I would capitalize the first letter of the property names here, but to keep it consistent with the JSON, they are lower case.
public class UserData
{
public string name { get; set; }
public string username { get; set; }
public int id { get; set; }
public string state { get; set; }
public string email { get; set; }
}
You will need to add a using for Json.NET
using Newtonsoft.Json;
Finally, you can load, and deserialize your data into a strongly typed list, which you can then use to populate your engineers dictionary.
string datapath = #"D:\project\Gitlap\EngineerEmail\jsonlist5.json";
Dictionary<string, string> engineers = new Dictionary<string, string>();
List<UserData> data = new List<UserData>();
using (StreamReader r = new StreamReader(datapath))
{
string json = r.ReadToEnd();
data = JsonConvert.DeserializeObject<List<UserData>>(json);
data.ForEach(engineer => engineers.Add(engineer.email, engineer.id.ToString()));
}
As mentioned in another answer, your JSON is also badly formed. This will need correcting before it will deserialize correctly. We just need to add a comma to separate the two objects, and wrap them both in a JSON array, with []
[
{
"name":"name1",
"username":"name1",
"id":505,
"state":"active",
"email":"name1#mail.com"
},
{
"name":"name2",
"username":"name2",
"id":504,
"state":"active",
"email":"name2#mail.com"
}
]
Improvements
As your Id field is an integer, it would be better to change your dictionary from
Dictionary<string, string> engineers = new Dictionary<string, string>();
into
Dictionary<string, int> engineers = new Dictionary<int, string>();
You will then be able to simplify your ForEach query slightly. The ForEach can also be moved outside of the using() block.
data.ForEach(engineer =>
engineers.Add(engineer.email, engineer.id));
Improved solution
This includes the improvements above, I've used var for brevity.
var datapath = #"D:\project\Gitlap\EngineerEmail\jsonlist5.json";
var engineers = new Dictionary<string, int>();
var data = new List<UserData>();
using (var r = new StreamReader(datapath))
{
var json = r.ReadToEnd();
data = JsonConvert.DeserializeObject<List<UserData>>(json);
}
data.ForEach(engineer =>
engineers.Add(engineer.email, engineer.id));
try to create class that represent the data in json object for example
Class obj
{
public int Id { get ; set; }
public string email { get ; set; }
public string username { get ; set; }
public string state { get ; set; }
public string email { get ; set; }
}
then
using System.Web.Script.Serialization;
var js = new JavaScriptSerializer();
List<obj> list = js.Deserialize<List<obj>>(jsonString);
after that you can access all list items id and email by using foreach

Noob using C# classes and accessing them

I'm working with JSON (using json.net) and a C# console application and I am trying to set some values for a JSON POST to a server.
I can set some of the values, but accessing others is giving me fits.
My JSON looks like this:
{
"params" : [
{
"url" : "sys/login/user",
"data" : [
{
"passwd" : "pwd",
"user" : "user"
}
]
}
],
"session" : 1,
"id" : 1,
"method" : "exec"
}
I ran that through json2csharp and it generated me the following classes.
public class Datum
{
public string passwd { get; set; }
public string user { get; set; }
}
public class Param
{
public string url { get; set; }
public List<Datum> data { get; set; }
}
public class RootObject
{
public List<Param> #params { get; set; }
public string session { get; set; }
public int id { get; set; }
public string method { get; set; }
}
I then created this object for testing in my Main method
RootObject temp = new RootObject()
temp.id = 1;
temp.method = "exec";
temp.session = "1";
and those parameters get set just fine.
I can also set the URL param using the following:
temp.#params.Add(new Param { url = "some/url", });
It is setting the public List<Datum> data { get; set; } item that is the problem. I cannot figure out how to access that and set the user and password items.
If I add this to the Param class I can set the values, but this seems to be the wrong way/place to me.
public Param()
{
data = new List<Datum>();
data.Add(new Datum { user = "user", passwd = "pass" });
}
Well, you create your RootObject like this:
RootObject temp = new RootObject()
temp.id = 1;
temp.method = "exec";
temp.session = "1";
Then you create the params list and fill it with one Param:
temp.#params = new List<Param>();
temp.#params.Add(new Param { url = "some/url" });
You can then set the data for one param in the list (in this example the first one):
temp.#params[0].data = new List<Datum>();
temp.#params[0].data.Add(new Datum { user = "user", passwd = "pass" });
This is necessary, because #params is a list of Param objects. You could also fill the data when creating the Param instance before adding it to the list (easier, because you otherwise need to know the list index).
temp.#params = new List<Param>();
Param p = new Param { url = "some/url" };
p.data = new List<Datum>();
p.data.Add(new Datum() { ... });
temp.#params.Add(p);
Usually you'd change change the default constructors to initialize the lists already and prevent the list instances from being replaced by changing the properties to read-only, but that might not work well with JSON deserialization, so you really need to try this. It would look like this:
public class Param
{
public Param()
{
data = new List<Datum>();
}
public string url { get; set; }
public List<Datum> data { get; private set; }
}
public class RootObject
{
public RootObject()
{
#params = new List<Param>();
}
public List<Param> #params { get; private set; }
public string session { get; set; }
public int id { get; set; }
public string method { get; set; }
}

Read JSON string as key value

I have following json:
{
"serverTime": "2013-08-12 02:45:55,558",
"data": [
{
"key1": 1,
"key2": {},
"key3": {
"key4": [
""
],
"key5": "test2"
},
"key7": 0
},
{
"key8": 1,
"key9": {},
"key10": {
"key4": [
""
],
"key9": "test2"
},
"key11": 0
}
]
}
I want to get values as key value pair. Something like:
jsonObject[data][0]
should give first item of the data array.
I am using JSONFx.net. But it gives strongly typed objects. I do not want that.
Is there any way to parse JSON as key value as I mentioned earlier?
Thanks
Try this:
using System;
using System.IO;
using Newtonsoft.Json;
class Program
{
static void Main(string[] args)
{
var json = File.ReadAllText("input.txt");
var a = new { serverTime = "", data = new object[] { } };
var c = new JsonSerializer();
dynamic jsonObject = c.Deserialize(new StringReader(json), a.GetType());
Console.WriteLine(jsonObject.data[0]);
}
}
If you're not averse to using Json.NET, you can do this:
var jsonString = #"
{
""serverTime"": ""2013-08-12 02:45:55,558"",
""data"": [
{
""key1"": 1,
""key2"": {},
""key3"": {
""key4"": [
""""
],
""key5"": ""test2""
},
""key7"": 0
},
{
""key8"": 1,
""key9"": {},
""key10"": {
""key4"": [
""""
],
""key9"": ""test2""
},
""key11"": 0
}
]
}";
var jsonResult = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(jsonString);
var firstItem = jsonResult["data"][0];
firstItem would be an array of the first item in the data array:
Hope this helps.
First create classes to parse string
public class Key2
{
}
public class Key3
{
public List<string> key4 { get; set; }
public string key5 { get; set; }
}
public class Key9
{
}
public class Key10
{
public List<string> key4 { get; set; }
public string key9 { get; set; }
}
public class Datum
{
public int key1 { get; set; }
public Key2 key2 { get; set; }
public Key3 key3 { get; set; }
public int key7 { get; set; }
public int? key8 { get; set; }
public Key9 key9 { get; set; }
public Key10 key10 { get; set; }
public int? key11 { get; set; }
}
public class RootObject
{
public string serverTime { get; set; }
public List<Datum> data { get; set; }
}
add reference of Newtonsoft.Json.dll
RootObject obj = JsonConvert.DeserializeObject<RootObject>(jsonData);
then you can access values .
If you want to do this without third party libraries then do:
I would use the following code:
var deserializer = new JavaScriptSerializer();
var someObject = deserializer.DeserializeObject(json);
string serverTime = someObject["serverTime"].ToString();
Dictionary<string, int> data = someObject["data"] as Dictionary<string, int>;
Give it a go.
Edit: You may need to change the last line to:
Dictionary<string, int?> data = someObject["data"] as Dictionary<string, int?>;

c# JavaScriptSerializer on JSON containing string + dictionary

May i know how to parsing the JSON stated as below..... the JSON is part of Yahoo OAuth Contacts list.
JSON:
"fields":[{
"id":2,
"type":"nickname",
"value":"Hello"
},
{
"id":3,
"type":"email",
"value":"MyTesting#hotmail.com"
},
{
"id":1,
"type":"name",
"value":{
"givenName":"Otopass",
"middleName":"Test",
"familyName":"Hotmail"
},
}],
C# object :
private class fields
{
public string id { get; set; }
public string type { get; set; }
//public string value { get; set; } //Stuck At Here !!!!
//public Dictionary<string, string> value { get; set; } //Stuck At Here !!!!
}
How to parse the "value"?? since it's combination type of String & Dictionary.
I can't answer it using JavaScriptSerializer but you can do it using json.Net and Linq
var jObj = JObject.Parse(json);
var fields = jObj["fields"]
.Select(x => new Field
{
Id = (int)x["id"],
Type = (string)x["type"],
Value = x["value"] is JValue
? new Dictionary<string,string>(){{"",(string)x["value"]}}
: x["value"].Children()
.Cast<JProperty>()
.ToDictionary(p => p.Name, p => (string)p.Value)
})
.ToList();
private class Field
{
public int Id { get; set; }
public string Type { get; set; }
public Dictionary<string, string> Value { get; set; }
}
PS: I fixed your partial json string as
string json =
#"{""fields"":[
{
""id"":2,
""type"":""nickname"",
""value"":""Hello""
},
{
""id"":3,
""type"":""email"",
""value"":""MyTesting#hotmail.com""
},
{
""id"":1,
""type"":""name"",
""value"":{
""givenName"":""Otopass"",
""middleName"":""Test"",
""familyName"":""Hotmail""
}
}
]}";

Categories

Resources