F
F
forever_alien20002020-08-28 13:13:03
JSON
forever_alien2000, 2020-08-28 13:13:03

Problem with getting data and json?

you need to get "records"
but the error is "Accessed JArray values ​​with invalid key value: "datasetid". Int32 array index expected"
here is the code:
var url = @" https://data.townofcary.org/api/records/1.0/ search... ";
varweb = new WebClient();
var source = JObject.Parse(web.DownloadString(url))["records"];
var item = new
{

recordid = (string)source["recordid"],

};
var input = JObject.FromObject(item).ToString();
richTextBox1.Text = input;

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Korotenko, 2020-08-28
@forever_alien2000

Check the structure, I would recommend unpacking it into a class and proceed further.
a7c09d081a.jpg
Are you sure that you need it?

var url = @"https://data.townofcary.org/api/records/1.0/search/?dataset=railroads&q=";
            var web = new WebClient();
            var source = JObject.Parse(web.DownloadString(url))["records"];
            var item = new
            {

                recordid = (string)source.First["recordid"],

            };
            var input = JObject.FromObject(item).ToString();
            return input;
            /// { "recordid": "c9b7df3efd65e8d545cc89f007988cd8c31b5465"

P
Peter, 2020-08-28
@petermzg

Because "records" is an array of objects. And you can get these array values ​​by index.
And you are trying (string)source["recordid"] to take data from the object itself.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question