0

I have a function where I can get all JSON data from but I do not understand how to pick out specific data.

I am using dynamic variable to loop thought all JSON information.

How can I for example pick out all "names:" from the given JSON URL and display it in the console.

Fetching data with url:

    public string FetchData(string url)
    {
        string String_jsonData;

        using (WebClient client = new WebClient())
        {
            Uri SourceUri = new Uri(url);

            String_jsonData = client.DownloadString(SourceUri);
        }
        return String_jsonData;
    }

Main and run methode,

    static void Main(string[] args)
    {
        Program p = new Program();
        p.run();
        Console.ReadKey();

    }

    public void run()
    {
        string url = "JSON URL;

        dynamic pers = JsonConvert.DeserializeObject(FetchData(url));

        // fetching all data. Here I want to select specific data and save it.
        foreach (var item in pers)
        {
            Console.WriteLine(item);
        }

    }

Model Class:

class Person
{
    public string name { get; set; }

    public string ci_list { get; set; }
}

Here is an example of the data I want to fetch

{
"items": [
{
"ci_type": "xxx",
"exists_in_equipman": false,
"exists_in_hydra": false,
"exists_in_taxen": true,
"hns_name": false,
"id": xxx,
"latest_sync_info": {
  "HYDRA": "xxx",
  "TAXEN": "xxx"
},
"modified_url": "xxx",
"name": "xxx",
"params": {
  "Band (4G)": {
    "id": xxx,
    "source": "script",
    "source_name": "xxx",
    "value": {}
  },
  "Hardware Specification": {
    "id": xxx,
    "source": "script",
    "source_name": "taxen_import",
    "value": {
      "0": {
        "3GPP release": {
          "value": ""
        },
        "Accessories": {
          "value": ""
        },
        "Category": {
          "value": ""
        },
        "Hw rev": {
          "value": "xxx"
        },
        "Locked": {
          "value": ""
        },
        "Model family name": {
          "value": "xxxx"
        },
        "Physical state": {
          "value": "xxxx"
        },
        "Serial No": {
          "value": "xxxx"
        },
        "Software": {
          "value": ""
        },
        "Software version": {
          "value": ""
        }
      }
    }
  },
10
  • 1
    Did you try Console.WriteLine(item.name);? Commented Mar 4, 2019 at 9:54
  • Can you hint as to what the json structure looks like? Is name a top level property? Commented Mar 4, 2019 at 9:55
  • Can you provide JSON getting from URL? Commented Mar 4, 2019 at 9:57
  • Do not use WebClient, it is very old. Use HttpClient instead, it is much more modern. Commented Mar 4, 2019 at 10:47
  • I have updated with JSon data. Commented Mar 4, 2019 at 11:02

2 Answers 2

1

First get data from url

var json = string.Empty;

string url = "http://";

using (var httpClient = new HttpClient())
{
    json = httpClient.GetStringAsync(url).Result;

}

Then Deserialize object (assume is lest of objects)

 List<dynamic> list = JsonConvert.DeserializeObject<List<dynamic>>(json);

foreach (var ar in list)
{
   Console.WriteLine(ar["name"]); //assume object look like {"name":"saa", "age":"23"} and want to access name 
}

Example in how to extract ci_type value based on json object in your question

string json = "{\r\n\"items\": [\r\n{\r\n\"ci_type\": \"xxx\",\r\n\"exists_in_equipman\": false,\r\n\"exists_in_hydra\": false\r\n}\r\n]\r\n}";
var result = JsonConvert.DeserializeObject<dynamic>(json);
var data  = result["items"];

foreach (var cityType in data)
{
    Console.WriteLine(cityType["ci_type"]);
}

result : xxx

Sign up to request clarification or add additional context in comments.

3 Comments

Hello. I am getting this error when implementing your solution. Newtonsoft.Json.JsonSerializationException HResult=0x80131500 Message=Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[System.Object]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
Please show me the json data you suppose to deserialize
Hello. I have posted the beginning part that I want to deserialize in the topic.
0

Your issue is that pers is not directly an array, it's an object.

You need to loop over the "items" property which is held within it. This array is what contains the objects which have the "name" properties.

Change your loop code to

foreach (var item in pers.items)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.