Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to access JSON object in C# [duplicate]

I receive the following Json through a web service:

  {      report: {       Id: "aaakkj98898983"      }   } 

I want to get value of the Id. How to do this in C#? THANKS

like image 945
Bathiya Ladduwahetty Avatar asked May 09 '13 10:05

Bathiya Ladduwahetty


People also ask

Can C read JSON files?

The website states the following: Jansson is a C library for encoding, decoding and manipulating JSON data. It features: Simple and intuitive API and data model. Can both encode to and decode from JSON.

How do I view a JSON object?

To access the JSON object in JavaScript, parse it with JSON. parse() , and access it via “.” or “[]”.


1 Answers

First, download Newtonsoft's Json Library, then parse the json using JObject. This allows you to access the properties within pretty easily, like so:

using System; using Newtonsoft.Json.Linq;  namespace testClient {     class Program     {         static void Main()         {             var myJsonString = "{report: {Id: \"aaakkj98898983\"}}";             var jo = JObject.Parse(myJsonString);             var id = jo["report"]["Id"].ToString();             Console.WriteLine(id);             Console.Read();         }     } }    
like image 167
Maloric Avatar answered Sep 20 '22 22:09

Maloric