0

I'm new to JSON so I hope you can help me. I did this:

var myfields = Newtonsoft.Json.JsonConvert.DeserializeObject<Rootobject>(s);

this is the link for the JSON: link As you can see I have many fields, each field is shown few times. Now I have access only to the first one. for example:

 final = myfields.results.First().warnings[0];

This String contains the first warnings field. I tried to put instead of 0 - 1 but I get an error.

How can I get the others?

Thanks

Ron F
  • 27
  • 1
  • 6
  • 1
    What's you tried ? Give we a sample of the json that you received and the expected output. – Neyoh Nov 11 '16 at 21:43
  • 1
    Please show your work. We can't help you solve your problem without seeing what issue you're having. – Soviut Nov 11 '16 at 21:53
  • A [mcve] demonstrating the problem would be required for us to help. See https://stackoverflow.com/help/how-to-ask. – dbc Nov 11 '16 at 22:02
  • In the absence of a [mcve], you could install [tag:json.net] then use [LINQ to JSON](http://www.newtonsoft.com/json/help/html/LINQtoJSON.htm). See [Can I LINQ a JSON?](https://stackoverflow.com/questions/18758361/can-i-linq-a-json). Or use `SelectToken` as explained in [Searching for a specific JToken by name in a JObject hierarchy](https://stackoverflow.com/questions/19645501). – dbc Nov 11 '16 at 22:25

1 Answers1

4

Create class with only fields you need and then deserialize json

public class YourTwoField
{
    [JsonProperty("field1")]
    public string FieldOne { get; set; }

    [JsonProperty("field2")]
    public string FieldTwo { get; set; }
}

var myfields = Newtonsoft.Json.JsonConvert.DeserializeObject<YourTwoField>(yourJsonString);
// use values
myfields.FieldOne

Update on comments:

Your problem was that You didn't provide enough information at the beginning. Fields you required is in the bottom level of JSON hierarchy.

  1. Copy your json result to the clipboard
  2. In Visual Studio open empty file
  3. Then Edit -> Paste Special -> Paste JSON as Classes

Now get all JSON schema as classes where I think will be generated some RootObject, then

var root = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(yourJsonString);

//Then your can brows to the values you want
root.results.First().openfda.generic_name
root.results.First().openfda.brand_name

Or loop all results

foreach(var result in root.results)
{
    result.openfda.generic_name;
    result.openfda.brand_name;
}
Fabio
  • 31,528
  • 4
  • 33
  • 72
  • Hi, Thank You everyone. this is for example the api results: [link](https://api.fda.gov/drug/label.json?search=brand_name:humalog&limit=15), I had issues with Deserialize the JSON. – Ron F Nov 12 '16 at 07:07
  • What kind of issues? and add your api result to the question – Fabio Nov 12 '16 at 07:24
  • Hi, I added a link in the comment before. I had problems with parsing selected fields, Now I see your tip and I'll try to use it. – Ron F Nov 12 '16 at 07:56
  • Hi this is what I did, [link](http://txt.do/d5fvg), this is the class [link](http://txt.do/d5fvu) and still It doesnt working, I'm getting thie error: An unhandled exception of type 'Newtonsoft.Json.JsonReaderException' occurred in Newtonsoft.Json.dll. I hope you can help me – Ron F Nov 12 '16 at 10:23
  • @RonF, Put all this information to the question - so you will get more help. And provide more information about exception you got - maybe Inner Exception message – Fabio Nov 12 '16 at 10:46
  • Thank You! I'm sorry but I dont understand what to do. I started a new thread more arranged. [link](http://stackoverflow.com/questions/40561970/deserialize-json-object-error) Can you help me there? – Ron F Nov 12 '16 at 11:40
  • There is the same answer as I have here – Fabio Nov 12 '16 at 11:40
  • Hi I'm facing now a new problem, I have access only to the first brand_name. How can I access the others? – Ron F Nov 12 '16 at 16:32