Getting '#' as prefix in each properties while converting SSRS XML Report into JSON using C#.
How can we remove unwanted '#' from every properties?
As SSRS does not support it's report to be downloaded into JSON format using URL. So, I am exporting the report first in XML format and then converting the report into JSON.
I am using Newtonsoft.JSON library to convert from XML to JSON.
//SSRS Report URL :-
_reportURL = http://<server-name>/reportserver?/<report-
name>/&rs:Format=Xml
// Getting byte array using HttpClient
_response = GetHttpClient().GetAsync(_reportURL).Result;
if (_response.IsSuccessStatusCode)
{
var data = _response.Content.ReadAsStreamAsync(); // Getting stream of
data from server response
XmlDocument xmlDocument = new XmlDocument();
//xml document by using Stream
using (data.Result)
{
xmlDocument.Load(data.Result);
}
// Deserializing xml node to json string
string jsonString = JsonConvert.SerializeXmlNode(xmlDocument,
Newtonsoft.Json.Formatting.Indented, false);
if (!jsonString.Equals(String.Empty)){
// Getting byte array string
byteArray = Encoding.ASCII.GetBytes(jsonString);
}
}
Actual Result :-
{
"?xml": {
"#version": "1.0",
"#encoding": "utf-8"
},
"Report": {
"#Name": "<report-name>",
"#xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
"#xmlns": "<report-name>",
"Tablix1": {
"Details_Collection": {
"Details": [
{
"#CompanyId": "60",
"#ClaimId": "1",
"#PolicyId": "2",
"#AgencyId": "1",
"#ClaimNumber": "WC2019-EGN-001",
"#PolicyNumber": "FAI100-1500-002",
"#ClaimantName": "Alan Owens",
"#MaritalStatus": "Married",
"#JurisdictionState": "California"
},
{
"#CompanyId": "70",
"#ClaimId": "2",
"#PolicyId": "4",
"#AgencyId": "3",
"#ClaimNumber": "WC2019-CCW-0401",
"#PolicyNumber": "CIC115-0200-301",
"#ClaimantName": "Maria Stevens",
"#MaritalStatus": "Single",
"#JurisdictionState": "Illinois"
}
]
}
}
}
}
Expected Result :-
{
"?xml": {
"version": "1.0",
"encoding": "utf-8"
},
"Report": {
"Name": "<report-name>",
"xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
"xmlns": "<report-name>",
"Tablix1": {
"Details_Collection": {
"Details": [
{
"CompanyId": "60",
"ClaimId": "1",
"PolicyId": "2",
"AgencyId": "1",
"ClaimNumber": "WC2019-EGN-001",
"PolicyNumber": "FAI100-1500-002",
"ClaimantName": "Alan Owens",
"MaritalStatus": "Married",
"JurisdictionState": "California"
},
{
"CompanyId": "70",
"ClaimId": "2",
"PolicyId": "4",
"AgencyId": "3",
"ClaimNumber": "WC2019-CCW-0401",
"PolicyNumber": "CIC115-0200-301",
"ClaimantName": "Maria Stevens",
"MaritalStatus": "Single",
"JurisdictionState": "Illinois"
}
]
}
}
}
}
Related
I am using netownsoft json.net to serlize an object but its adding string at the start I dont under stand why its doing this.
Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: <. Path '', line 0, position 0.
public async Task<T> GetDataFromSageService<T>(string url, params string[] args)
where T : class
{
var uri = new Uri(string.Format(url, args));
var response = await _client.GetAsync(uri);
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(content);
}
return default(T);
}
I am using the following to encode my end point which is hosted in a wcf service.
public string GetWarehouses()
{
DataSet ds = new SqlDa().GetWarehouses();
ds.Tables[0].TableName = "Warehouses";
return JsonConvert.SerializeObject(ds, Formatting.Indented);
}
But the string i am getting back is as such
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
{
"Warehouses": [
{
"WarehouseID": 13016489,
"Name": "B",
"Description": "Belfast "
},
{
"WarehouseID": 13016647,
"Name": "B",
"Description": "B"
},
{
"WarehouseID": 13815467,
"Name": "Direct Delivery",
"Description": ""
},
{
"WarehouseID": 1008,
"Name": "PW",
"Description": "Postal Way"
},
{
"WarehouseID": 13016234,
"Name": "H",
"Description": "Hospital"
},
{
"WarehouseID": 13016238,
"Name": "MPC",
"Description": "Clinic"
},
{
"WarehouseID": 13029366,
"Name": "O",
"Description": "Outpatient"
},
{
"WarehouseID": 13815466,
"Name": "Returns",
"Description": ""
}
]
}
</string>
As You can see its enclosed it as a string for some reason and don't understand as to why. Is their a way with the data set to make sure that it gets converted into proper json.
If you don't want to modify your server code, you could use regex to extract legal json string from your response.
string content ="your context with xml"
Regex regex = new Regex("<string\\s*xmlns=\".*\">([\\s\\S]*)</string>");
Match match = regex.Match(content);
Response.Write(match.Groups[1].Value);
Newtonsoft.Json.JsonConvert.DeserializeObject(match.Groups[1].Value);
i am inserting json format but need to know how i can insert in this format as mention below
{
"products": [
{
"product_id": "55",
"name": "testing data",
"price": "77",
"total": "77",
"quantity": "1",
"type": "kg"
}],
],
"totals": [
{
"code": "sub_total",
"title": "Sub-Total",
"text": "Rs277.00",
"value": "277.0000",
"sort_order": "1"
}]
}
here is my code which i am trying
items local = new items();
foreach (var item in _LocalItem){
local = new items
{
name = item.name
};
}
var json = JsonConvert.SerializeObject(local);
var request = new HttpRequestMessage(HttpMethod.Post, "http://orangepotato.rjcpacking.com/index.php?route=api/login/addcustomerOrder");
request.Content = new StringContent(json);
i dont understand where i can add "products" array in json format
Simply: don't serialize an array - serialize something that has an array in a member called products:
var json = JsonConvert.SerializeObject(new { products = local });
I want to make a RestSharp request and the following desired result is like :
tes = {
"paye": "0",
"type": "0",
"lines": [
{
"desc": "1",
"note": "10",
},
{
"desc": "2",
"note": "20",
},
{
"desc": "3",
"note": "30",
}
]
}
so I make a request like this :
var client = new RestClient(url);
var request = new RestRequest("tes", Method.POST);
request.AddParameter("paye", 0);
request.AddParameter("type", 0);
and the problem is how to finish the lines[] part?
Thank you
Create class like :
class tes {
//include all the properties here
}
Serialize the object and pass it in parameter with the RestSharp request.
I have a Json file as shown below. I want to convert only the option node in the JSON file into a C# ListItemCollection object
ASP.net File:
var path = Server.MapPath(#"~/json.json");
using (StreamReader r = new StreamReader(path,false))
{
string json = r.ReadToEnd();
dynamic arr = JsonConvert.DeserializeObject(json);
ListItemCollection licRB = new ListItemCollection();
licRB = arr.AnswerRadioButton.option;<<-- run time error is produced here
}
JSON File:
{ "FormTitle": "This is Form Title from JSON",
"TitleQuestion1": "This is the Title of Question 1",
"TextQuestion1": "1- This is the text of Quextion Number 1",
"AnswerRadioButton": {
"visible": "true",
"title": "Radio Button Title",
"FieldsetRBStyle": { "border": "1px" },
"option" : [
{
"text": "text1",
"value": "v1",
"checked": "false"
},
{
"text": "text2",
"value": "v2",
"checked": "true"
},
{
"text": "text3",
"value": "v3",
"checked": "false"
},
{
"text": "text4",
"value": "v4",
"checked": "true"
}
] }}
Assuming that you have the required classes (e.g. using this tool) you can access the options like this:
var test = JsonConvert.DeserializeObject<RootObject>(json);
var options = test.AnswerRadioButton.option;
I am trying to create JSON array using Newtonsoft JSON API but its giving me error. I want to achieve structure like
[
{
"id":"26",
"appsurvey":"1",
"fk_curriculumid":"70",
"status":"Completed",
"lastaccessedon":"2014-06-20 09:18:54",
"questions":[
{
"feedback":"6",
"questionid":"1"
},
{
"feedback":"8",
"questionid":"2"
},
{
"feedback":"1",
"questionid":"3"
}
],
"fk_clientid":"24",
"learnerid":"260"
}
]
I want ot add questions array for multiple time but it is giving me error
Can not add property questions to Newtonsoft.Json.Linq.JObject. Property with the same name already exists on object.
Here is my code:
JArray surveytrackingA = new JArray();
/*code to add
[
{"id":"26",
"appsurvey":"1",
"fk_curriculumid":"70",
"status":"Completed",
"lastaccessedon":"2014-06-20 09:18:54"}]
*/
for (int i = 0; i < surveytrackingA.Count; i++)
{
JObject surveytrackD = (JObject)surveytrackingA[i];
string queryOne = "select * from table101 where fk_curriculumid='"
+ surveytrackD["fk_curriculumid"].ToString()
+ "' and fk_surveyid='"
+ surveytrackD["appsurvey"].ToString() + "'";
JArray questionsA = new JArray();
using (var stmt = await App.localDB.PrepareStatementAsync(queryOne))
{
while (await stmt.StepAsync())
{
JObject questionD = new JObject();
questionD.Add("questionid", stmt.GetTextAt(5));
questionD.Add("feedback", stmt.GetTextAt(6));
questionsA.Add(questionD);
}
}
surveytrackD.Add("questions", questionsA); /*error occurred here when second question array is getting inserted in surveyTrackD*/
surveytrackingA.Add(surveytrackD);
}
Can anyone please correct me. Thanks in advance.
Update:
surveytrackD have the json data,
{
"fk_clientid": "24",
"learnerid": "260",
"appsurvey": "1",
"id": "26",
"fk_curriculumid": "70",
"status": "completed",
"lastaccessedon": "2014-06-20 09:18:54"
}
You can achieve the same result (JArray in JArray) using regular C# classes and at the end serialize to JSon.
I posted a sample in Github; here a fragment of the code that produces your expected output:
var Surveys = new List<SurveytrackD>();
Surveys.Add( new SurveytrackD { id = "26", appsurvey = "1", fk_curriculumid = "70", status = "Completed", learnerid = "240" } );
Surveys.Add( new SurveytrackD { id = "27", appsurvey = "1", fk_curriculumid = "71", status = "Completed", learnerid = "241" });
foreach (var survey in Surveys)
{
survey.questions = new List<Question>();
survey.questions.Add(new Question { questionid = "1", feedback = "0" });
survey.questions.Add(new Question { questionid = "2", feedback = "1" });
}
var json = JsonConvert.SerializeObject(Surveys, Formatting.Indented);
Console.WriteLine(json);
The output is:
[
{
"fk_clientid": null,
"learnerid": "240",
"appsurvey": "1",
"id": "26",
"fk_curriculumid": "70",
"status": "Completed",
"lastaccessedon": null,
"questions": [
{
"feedback": "0",
"questionid": "1"
},
{
"feedback": "1",
"questionid": "2"
}
]
},
{
"fk_clientid": null,
"learnerid": "241",
"appsurvey": "1",
"id": "27",
"fk_curriculumid": "71",
"status": "Completed",
"lastaccessedon": null,
"questions": [
{
"feedback": "0",
"questionid": "1"
},
{
"feedback": "1",
"questionid": "2"
}
]
}
]