I'm trying to fetch Symptoms from an API. I can fetch them already and I'm serializing it inside a class succesfully. The result looks like in image that i share at below:
There are just IDs and Names inside them. The second table from API is like that:
So here my Entity Class;
using System.Collections.Generic;
public class SymptomousInBodySublocations
{
public int ID { get; set; }
public string Name { get; set; }
public bool HasRedFlag { get; set; }
public ICollection<BodyLocations> HealthSymptomLocationIDs { get; set; }
public string ProfName { get; set; }
public List<string> Synonyms { get; set; }
}
And my Serialize Method:
public static List<SymptomousInBodySublocations> SymptomsInBodySublocations()
{
var client = new RestClient("https://priaid-symptom-checker-v1.p.rapidapi.com/symptoms/31/man?format=json&language=en-gb");
var request = new RestRequest(Method.GET);
request.AddHeader("x-rapidapi-host", "priaid-symptom-checker-v1.p.rapidapi.com");
request.AddHeader("x-rapidapi-key", "<api-key>");
List<SymptomousInBodySublocations> SymptomsInBodySublocationsList = new List<SymptomousInBodySublocations>();
var response = client.Execute<List<SymptomousInBodySublocations>>(request);
foreach(SymptomousInBodySublocations variables in response.Data)
{
SymptomsInBodySublocationsList.Add(variables);
}
return SymptomsInBodySublocationsList;
}
And my BodyLocations Class:
public class BodyLocations
{
public int ID { get; set; }
public string Name { get; set; }
}
In this point when i tried to fetch my data inside my List<BodyLocations>() the response.Data is empty. What should i do?
HealthSymptomLocationIDs isn't an object, looking at the response it is an array of integers.
Changing the field to match the response should populate the field with the integer values from the API
public List<int> HealthSymptomLocationIDs { get; set; }
Related
Hey all I am trying to figure out how to go about saving just one value in my JSON class instead of having to write the whole JSON out again with "New". I am using the Newton JSON.Net.
This is my JSON structure:
public class GV
{
public class Data
{
[JsonProperty("pathForNESPosters")]
public static string PathForNESPosters { get; set; }
[JsonProperty("pathForSNESPosters")]
public static string PathForSNESPosters { get; set; }
[JsonProperty("pathForSEGAPosters")]
public static string PathForSEGAPosters { get; set; }
[JsonProperty("pathToNESContent")]
public static string PathToNESContent { get; set; }
[JsonProperty("pathToSNESContent")]
public static string PathToSNESContent { get; set; }
[JsonProperty("pathToSEGAContent")]
public static string PathToSEGAContent { get; set; }
[JsonProperty("lastSavedVolume")]
public static double LastSavedVolume { get; set; }
}
public class Root
{
public Data data { get; set; }
}
And I have no issues with loading that data from a file into my class:
GV.Root myDeserializedClass = JsonConvert.DeserializeObject<GV.Root>(File.ReadAllText(
currentAssemblyPath + String.Format(#"\Resources\{0}", "dataForLinks.json")
));
But I have yet to find anything searching that will let me do one update to an object in the class without wiping it out doing a New statement.
What I am wanting to do is something like the following:
-Load the json into my class object [Done]
-Save a value thats in my class object [stuck here]
GV.pathToNESContent = "new value here";
-Save class object (with the one new value) back to the file for which it came from preserving the other original values. [not here yet]
When I update just that one class object I am wanting to contain the original values for all the other JSON data I read in from the file.
Anyone have a good example of the above you can share?
update
I'd ditch the inner class structure:
namespace GV
{
public class Data
{
[JsonProperty("pathForNESPosters")]
public string PathForNESPosters { get; set; }
[JsonProperty("pathForSNESPosters")]
public string PathForSNESPosters { get; set; }
[JsonProperty("pathForSEGAPosters")]
public string PathForSEGAPosters { get; set; }
[JsonProperty("pathToNESContent")]
public string PathToNESContent { get; set; }
[JsonProperty("pathToSNESContent")]
public string PathToSNESContent { get; set; }
[JsonProperty("pathToSEGAContent")]
public string PathToSEGAContent { get; set; }
[JsonProperty("lastSavedVolume")]
public double LastSavedVolume { get; set; }
}
public class Root
{
public Data Data { get; set; }
}
Deser (use Path.Combine to build paths, not string concat):
var x = JsonConvert.DeserializeObject<GV.Root>(File.ReadAllText(
Path.Combine(currentAssemblyPath, "Resources", "dataForLinks.json"))
));
Edit:
x.Data.PathToNESContent = "...";
and re-ser
I would like to deserialize the following JSON (using Json.NET) to an object, but cannot, as the class name would need to begin with a number.
An example of this is the Wikipedia article API. Using the API to provide a JSON response returns something like this. Note the "16689396" inside the "pages" key.
{
"batchcomplete":"",
"continue":{
"grncontinue":"0.893378504602|0.893378998188|35714269|0",
"continue":"grncontinue||"
},
"query":{
"pages":{
"16689396":{
"pageid":16689396,
"ns":0,
"title":"Jalan Juru",
"extract":"<p><b>Jalan Juru</b> (Penang state road <i>P176</i>) is a major road in Penang, Malaysia.</p>\n\n<h2><span id=\"List_of_junctions\">List of junctions</span></h2>\n<p></p>\n<p><br></p>"
}
}
}
}
How could I deserialize this JSON containing a number which changes based on the article?
It sounds like the Pages property in your Query class would just need to be a Dictionary<int, Page> or Dictionary<string, Page>.
Complete example with the JSON you've provided - I've had to guess at some of the name meanings:
using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
public class Root
{
[JsonProperty("batchcomplete")]
public string BatchComplete { get; set; }
[JsonProperty("continue")]
public Continuation Continuation { get; set; }
[JsonProperty("query")]
public Query Query { get; set; }
}
public class Continuation
{
[JsonProperty("grncontinue")]
public string GrnContinue { get; set; }
[JsonProperty("continue")]
public string Continue { get; set; }
}
public class Query
{
[JsonProperty("pages")]
public Dictionary<int, Page> Pages { get; set; }
}
public class Page
{
[JsonProperty("pageid")]
public int Id { get; set; }
[JsonProperty("ns")]
public int Ns { get; set; }
[JsonProperty("title")]
public string Title { get; set; }
[JsonProperty("extract")]
public string Extract { get; set; }
}
class Test
{
static void Main()
{
string text = File.ReadAllText("test.json");
var root = JsonConvert.DeserializeObject<Root>(text);
Console.WriteLine(root.Query.Pages[16689396].Title);
}
}
Related question: Json deserialize from wikipedia api with c#
Essentially you need to changes from using a class for the pages to a dictionary, which allows for the dynamic nature of the naming convention.
Class definitions :
public class pageval
{
public int pageid { get; set; }
public int ns { get; set; }
public string title { get; set; }
public string extract { get; set; }
}
public class Query
{
public Dictionary<string, pageval> pages { get; set; }
}
public class Limits
{
public int extracts { get; set; }
}
public class RootObject
{
public string batchcomplete { get; set; }
public Query query { get; set; }
public Limits limits { get; set; }
}
Deserialization :
var root = JsonConvert.DeserializeObject<RootObject>(__YOUR_JSON_HERE__);
var page = responseJson.query.pages["16689396"];
You can implement your own DeSerializer or editing the JSON before you DeSerialize it.
I'm using c#
one of the mongo db document is in such structure
Class QuestionData{
public Guid Id { get; set; }
public string Type { get; set; }
public List<NotesQuestion> Question { get; set; }
}
So I want to do a updating. I write the update as following:
var update = Update.Set("Question", data.Question);
The 'data' is a type of QuestionData.
But now it says "invalid argument" of data.Question.
If I change it to
var update = Update.Set("Question", data.Question.ToJson());
It has no problem. But I don't want to save it as json string.
How to resolve this issue?
With my own model classes,
internal class QuestionData
{
[BsonId] public Guid Id { get; set; }
[BsonElement("type")] public string Type { get; set; }
[BsonElement("qnotes")] public QuestionNote[] QuestionNotes { get; set; }
}
[BsonNoId] internal class QuestionNote
{
[BsonElement("q")] public string Question { get; set; }
[BsonElement("n")] public string Note { get; set; }
}
you can overwrite the whole bson array like:
// get client, database, collection ...
var col = Database.GetCollection<QuestionData>("questions");
// QuestionNote[] toModifyWith = ...
var udb = Builders<QuestionData>.Update;
var fdb = Builders<QuestionData>.Filter;
var ur = await col.UpdateManyAsync(fdb.Empty, udb.Set(x => x.QuestionNotes, toModifyWith ));
I am getting some post data from an HTML form. The data that comes up is used to build a TextualReport object. I want to parse the various pieces of form data that come up and populate those parts of my model.
The reason i am not using a model binder is this is a fairly complex object and indices of my lists can be missing (start at 2, skip 3 etc..). I am starting with an existing object, and only replacing the properties that came up in the post.
I have the following object called TextualReport.
public class TextualReport {
public IList<Section> Sections { get; set; }
public IList<string> MiscInputs { get; set; }
public TextualReport(){
Sections = new List<Section>();
MiscInputs = new List<string>();
}
}
public class Section{
public Section(){
Id = Guid.NewGuid();
Blocks = new List<Block>();
}
public Guid Id { get; set; }
public string Heading { get; set; }
public IList<Block> Blocks { get; set; }
}
public class Block{
public Block(){
PreGraphText = new List<string>();
PreGraphInput = new List<string>();
Graphs = new List<Graph>();
PostGraphText = new List<string>();
}
public Guid Id { get; set; }
public string Heading { get; set; }
public IList<string> PreGraphText { get; set; }
public IList<Graph> Graphs { get; set; }
public IList<string> PostGraphText { get; set; }
public IList<string> PreGraphInput { get; set; }
}
public class Graph{
public string Url { get; set; }
public string Caption { get; set; }
}
From my HTML/Razor form, I get a sequence of text strings like the following:
TextualReport.Sections[1].Blocks[0].Graphs[0].Caption=
TextualReport.Sections[1].Blocks[0].Graphs[1].Caption=
TextualReport.Sections[1].Blocks[0].Graphs[2].Caption=
TextualReport.Sections[1].Blocks[0].PreGraphInput[0]=
TextualReport.Sections[1].Blocks[0].PreGraphInput[1]=
TextualReport.Sections[1].Blocks[1].Graphs[0].Caption=
TextualReport.Sections[1].Blocks[1].PreGraphInput[0]=
TextualReport.Sections[1].Blocks[1].PreGraphInput[1]=
TextualReport.Sections[1].Blocks[2].Graphs[0].Caption=
TextualReport.Sections[1].Blocks[2].Graphs[1].Caption=
TextualReport.Sections[1].Blocks[2].PreGraphInput[0]=
TextualReport.Sections[1].Blocks[2].PreGraphInput[1]=
TextualReport.Sections[1].Blocks[3].Graphs[0].Caption=
TextualReport.Sections[1].Blocks[3].PreGraphInput[0]=
TextualReport.Sections[1].Blocks[3].PreGraphInput[1]=
TextualReport.Sections[1].Blocks[4].Graphs[0].Caption=
TextualReport.Sections[1].Blocks[4].PreGraphInput[0]=
TextualReport.Sections[1].Blocks[4].PreGraphInput[1]=
What would be the best approach to populate my existing data with the various pieces that came up in the form post? I get the model from the database, then update based on the new data.
I am trying to display data collected from RestSharp. Just for example i have the following code but don't know how to display the data as it currently is.
private void readJSON()
{
string url = "http://www.jsonurl.com";
var restClient = new RestClient(url);
var request = new RestRequest(Method.GET);
//82.147.22.3
//What we are requesting:value
request.AddParameter("apikey", "xxxxxtheapikeygoesherexxxxx");
restClient.ExecuteAsync<Entry>(request, response =>
{
//What to do with the JSON?
});
}
I know i need to place the JSON between the ExecuteAsync<>() but i want to be able to take the data and for example place it into a listbox. Below is an example of the result given back to me from JSONtoSharp.com. code:
public class Change
{
public string direction { get; set; }
public int amount { get; set; }
public int actual { get; set; }
}
public class itementry
{
public int position { get; set; }
public int prePosition { get; set; }
public int Weeks { get; set; }
public string ar { get; set; }
public string ti { get; set; }
public Change ch { get; set; }
}
public class RootObject
{
public int charDate { get; set; }
public int retrieved { get; set; }
public List<Entry> entries { get; set; }
}
I am sure the answer is simple as glass and I just need help as i am completely lost in this one.. cant find any good documentation to help me out!
Note: This is for C# on Windows Phone 7 using RestSharp and Newtonsoft
restClient.ExecuteAsync<Entry>(request, response =>
{
//Supply your JSON data to a callback
Callback(response.Data);
});
public void Callback(string jsonResponse)
{
var responseList = JsonConvert.DeserializeObject<RootObject>(jsonResponse);
//Assuming you have properly setup binding properties for Listbox, databind listbox here
YourListBox.ItemsSource = responseList.entries;
}
Here JsonConvert is from NewtonSoft package