0

so I have a JSON text and I need to Extract Value of some fields from it

{"data":{"shortcode":{"id":"id123","shortcode":"alpha1","by":{"page_info":{"has_next_page":true,"cursor":"sometext=="},"edges":[{"node":{"id":"id1234","username":"admin123","full_name":"admin name",}},{"node":{"id":"id4321","username":"user123","full_name":"user ",}}]}}},"status":"ok"}

now I need the value of every single "username" field and "cursor" from it

I'm new to this topic and I appreciate any kind of help

2 Answers2

1

You could install Newtonsoft.Json, as a Nuget package, if you don't already have it installed in your project. Then use JsonConvert.DeserializeObject() method to parse it into a dynamic object type, using which you can access all its fields/properties.

Code would look something like this-

Add referenceto Newtonsoft.Json

using Newtonsoft.Json;

DeserializeObject method to parse a string into dynamic type (dynamic can be used instead of var)

var obj = JsonConvert.DeserializeObject<dynamic>(text);

For cursor field mentioned in your example

Console.WriteLine(obj.data.shortcode.by.page_info.cursor);

Iterate for each username mentioned your example

foreach (var edge in obj.data.shortcode.by.edges)
{
    Console.WriteLine(edge.node.id);
}

NOTE- Because you are using a dynamic type, you will have to be sure that you check for null values to avoid any "NullRefereneException".

saratcsss
  • 41
  • 2
-1

Well you can make a new class that match the structure of your json then deserialize the json into that class.

coposaja
  • 61
  • 4