I'm using JavaScriptSerializer and when I try to deserialize a JSON object as
Json.Deserialize<List<HeyWatchVideo>>(Request("video"));
I get following Exception:
MissingMethodException: No parameterless constructor defined for type of 'System.String'.
I tried matching my .NET classes, and the types of their properties just as close to my json object, but still getting above exception
My JSON object:
[{
"url": "http://media.heywatch.com.s3.amazonaws.com/14/14/a0f356a48381092e6e2a34021ce86b19/11002247",
"specs": {
"size": 3808,
"video": {
"fps": 11.63,
"height": 360,
"length": 61,
"width": 640,
"aspect": 1.78,
"codec": "mpeg4",
"container": "mov",
"rotation": 0,
"bitrate": 507,
"pix_format": "yuv420p",
"stream": 0.1
},
"thumb": "http://media.heywatch.com.s3.amazonaws.com/14/14/ad59fa501d1b9e826552dfc010cf1c98/11002247.jpg",
"audio": {
"sample_rate": 11025,
"channels": 1,
"codec": "aac",
"bitrate": 38,
"synched": true,
"stream": 0
},
"mime_type": "video/mp4"
},
"title": "elves.mp4",
"filename": "11002247",
"link": "http://heywatch.com/video/21091957.bin",
"updated_at": "2013-01-14T15:44:53+01:00",
"created_at": "2013-01-14T15:44:53+01:00",
"id": 21091957
}]
My Classes
public class HeyWatchVideo
{
public DateTime Created_At { get; set; }
public string Title { get; set; }
public Dictionary<string, string> Specs { get; set; }
public DateTime Updated_At { get; set; }
public int Id { get; set; }
public string Filename { get; set; }
public string Link { get; set; }
public string Url { get; set; }
}
public class HeyWatchVideoSpecs
{
public HeyWatchVideoSpecsAudio Audio { get; set; }
public HeyWatchVideoSpecsVideo Video { get; set; }
public string Thumb { get; set; }
public string Mime_type { get; set; }
public int Size { get; set; }
}
public class HeyWatchVideoSpecsVideo
{
public int Rotation { get; set; }
public double Aspect { get; set; }
public string Container { get; set; }
public string Codec { get; set; }
public int Length { get; set; }
public int Width { get; set; }
public int Bitrate { get; set; }
public string Pix_format { get; set; }
public double Fps { get; set; }
public double Stream { get; set; }
public int Height { get; set; }
}
public class HeyWatchVideoSpecsAudio
{
public int Channels { get; set; }
public int Sample_rate { get; set; }
public string Codec { get; set; }
public bool Synched { get; set; }
public int Bitrate { get; set; }
public int Stream { get; set; }
}
What am I doing wrong here?
My problem was solved. My .Net Classes didn't match exactly as Json object. (Both are in the question)
When they were matched, this error went away
Related
I'm trying to get the amount of pieces belonging to a certain Lego set using the Brickset API (https://brickset.com/article/52664/api-version-3-documentation)
When writing the Json string to the console it displays the amount of pieces correct. Howeever after deserializing and then writing only the value of pieces to the console it displays 0. All other properties are also not displayed when written to the console.
Result after writing the Json string to the console
{"status":"success","matches":1,"sets":\[
{
"setID": 31844,
"number": "10293",
"numberVariant": 1,
"name": "Santa's Visit",
"year": 2021,
"theme": "Icons",
"themeGroup": "Model making",
"subtheme": "Winter Village Collection",
"category": "Normal",
"released": true,
"pieces": 1445,
"minifigs": 4,
"image": {
"thumbnailURL": "https://images.brickset.com/sets/small/10293-1.jpg",
"imageURL": "https://images.brickset.com/sets/images/10293-1.jpg"
},
"bricksetURL": "https://brickset.com/sets/10293-1",
"collection": {},
"collections": {
"ownedBy": 9350,
"wantedBy": 2307
},
"LEGOCom": {
"US": {
"retailPrice": 99.99,
"dateFirstAvailable": "2021-09-17T00:00:00Z"
},
"UK": {
"retailPrice": 89.99,
"dateFirstAvailable": "2021-09-17T00:00:00Z"
},
"CA": {
"retailPrice": 139.99,
"dateFirstAvailable": "2021-09-17T00:00:00Z"
},
"DE": {
"retailPrice": 99.99,
"dateFirstAvailable": "2021-09-17T00:00:00Z"
}
},
"rating": 4.3,
"reviewCount": 0,
"packagingType": "Box",
"availability": "LEGO exclusive",
"instructionsCount": 15,
"additionalImageCount": 13,
"ageRange": {
"min": 18
},
"dimensions": {
"height": 28.0,
"width": 47.9,
"depth": 8.7,
"weight": 1.656
},
"barcode": {
"EAN": "5702016914313"
},
"extendedData": {
"tags": \[
"Santa Claus|n",
"18 Plus",
"Baked Goods",
"Bedroom",
"Bird",
"Brick Built Tree",
"Brick Separator",
"Christmas",
"Christmas Tree",
"D2c",
"Fireplace",
"Furniture",
"House",
"Kitchen",
"Light Brick",
"Mail",
"Microscale",
"Musical",
"Rocket",
"Seasonal",
"Winter Village"
\]
},
"lastUpdated": "2022-10-03T08:24:39.49Z"
}
\]}
Main Code
class Program
{
static async Task Main(string[] args)
{
await askSetNumber();
}
private async Task GetPosts(string url)
{
HttpClient client = new HttpClient();
string response = await client.GetStringAsync(url);
Console.WriteLine(response);
var set = JsonConvert.DeserializeObject<Rootobject>(response);
Console.WriteLine(set.pieces);
}
static async Task askSetNumber()
{
Console.WriteLine("Please enter a setnumber: ");
string setNumber = "{'setNumber':'" + Console.ReadLine().ToString() + "-1'}";
string url = "https://brickset.com/api/v3.asmx/getSets?apiKey=[APIKey here]&userHash=¶ms=" + setNumber;
Console.WriteLine(url);
Program program = new Program();
await program.GetPosts(url);
}
}
I made all classes by Pasting the Json as classes, This is the class of the object I need the data off
public class Rootobject
{
public int setID { get; set; }
public string number { get; set; }
public int numberVariant { get; set; }
public string name { get; set; }
public int year { get; set; }
public string theme { get; set; }
public string themeGroup { get; set; }
public string subtheme { get; set; }
public string category { get; set; }
public bool released { get; set; }
public int pieces { get; set; }
public int minifigs { get; set; }
public Image image { get; set; }
public string bricksetURL { get; set; }
public Collection collection { get; set; }
public Collections collections { get; set; }
public Legocom LEGOCom { get; set; }
public float rating { get; set; }
public int reviewCount { get; set; }
public string packagingType { get; set; }
public string availability { get; set; }
public int instructionsCount { get; set; }
public int additionalImageCount { get; set; }
public Agerange ageRange { get; set; }
public Dimensions dimensions { get; set; }
public Barcode barcode { get; set; }
public Extendeddata extendedData { get; set; }
public DateTime lastUpdated { get; set; }
}
I tried the example from How to get some values from a JSON string in C#? but set.pieces keeps returning 0.
This is my first time trying this kind of stuff, but I am stuck on this part.
Rootobject does not contain the same information as the json string you outputted to the console (it also escapes square brackets for some reason), so you either need to specify the entire object or navigate the json tree to get the value you require.
This is not a particularly good or robust solution, but you could replace the var set portion of you code with:
JsonDocument doc = JsonDocument.Parse(response);
if (!doc.RootElement.TryGetProperty("sets", out JsonElement sets)) return;
foreach(var set in sets.EnumerateArray())
{
if (!set.TryGetProperty("pieces", out JsonElement pieces)) continue;
Console.WriteLine(pieces.GetInt32());
}
(You'll also need using System.Text.Json; rather than the newtonsoft one.)
you have you json at first,since you have some strange "/" chars, after this you have to fix your classes too
json=json.Replace("\\[","[").Replace("\\]","]");
Set set = JsonConvert.DeserializeObject<Set>(json);
public class Set
{
public string status { get; set; }
public int matches { get; set; }
public List<SetItem> sets { get; set; }
}
public class SetItem
{
public int setID { get; set; }
public string number { get; set; }
public int numberVariant { get; set; }
public string name { get; set; }
public int year { get; set; }
public string theme { get; set; }
public string themeGroup { get; set; }
public string subtheme { get; set; }
public string category { get; set; }
public bool released { get; set; }
public int pieces { get; set; }
public int minifigs { get; set; }
public Image image { get; set; }
public string bricksetURL { get; set; }
public Collection collection { get; set; }
public Collections collections { get; set; }
public LEGOCom LEGOCom { get; set; }
public double rating { get; set; }
public int reviewCount { get; set; }
public string packagingType { get; set; }
public string availability { get; set; }
public int instructionsCount { get; set; }
public int additionalImageCount { get; set; }
public AgeRange ageRange { get; set; }
public Dimensions dimensions { get; set; }
public Barcode barcode { get; set; }
public ExtendedData extendedData { get; set; }
public DateTime lastUpdated { get; set; }
}
public class LEGOCom
{
public Country US { get; set; }
public Country UK { get; set; }
public Country CA { get; set; }
public Country DE { get; set; }
}
public class AgeRange
{
public int min { get; set; }
}
public class Barcode
{
public string EAN { get; set; }
}
public class Country
{
public double retailPrice { get; set; }
public DateTime dateFirstAvailable { get; set; }
}
and maybe it make some sense to define LEGOCom as a Dictionary, if you have much more countries
public Dictionary<string,Country> LEGOCom { get; set; }
I'm having a problem that raises an exception Newtonsoft.Json.JsonSerializationException in Newtonsoft.Json.dll when trying to deserialize an Json formatted string to an user defined object. The string that I'm reading from the file has been written before using the same object.
This is the code of deserielization (exception at the last line).
storageFile = await storageFolder.GetFileAsync(filePath);
string JsonDB = await FileIO.ReadTextAsync(storageFile);
MyDB myDB = Newtonsoft.Json.JsonConvert.DeserializeObject<MyDB>(JsonDB);
Here is the object class (Tag and Astro are enums):
public class MyDB
{
public string Version;
public DateTime Date;
public List<PhotoSpot_v0_1> Lista_v0_1;
}
public enum version
{
v0_1
}
public class PhotoSpot_v0_1
{
public int ID { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public int Rating { get; set; }
public bool Reminder { get; set; }
public bool Toast { get; set; }
private double Latitude { get; set; }
private double Longitude { get; set; }
private double Altitude { get; set; }
public Geopoint Geopoint { get; set; }
public Geopoint Landmark { get; set; }
public TimeZoneInfo TimeZone { get; set; }
public string MainTag { get; set; }
public version Version { get; set; }
public Landmark_v0_1 Landmarks { get; set; }
public List<Image_v0_1> Images { get; set; }
public List<URL_v0_1> URLs { get; set; }
public List<globalVars.Tag> Tags { get; set; }
public List<Date_v0_1> Dates { get; set; }
public bool Downloaded { get; set; }
public bool Exportable { get; set; }
}
public class Landmark_v0_1
{
public bool freeLandmark { get; set; }
public string Title { get; set; }
private double Latitude { get; set; }
private double Longitude { get; set; }
private double Altitude { get; set; }
public globalVars.Astro Astro { get; set; }
}
public class Image_v0_1
{
public string imageURI { get; set; }
public bool isDownloaded { get; set; }
}
public class URL_v0_1
{
public string sURL { get; set; }
}
public class Tag_v0_1
{
//public string TagName { get; set; }
public globalVars.TagImageURL TagName { get; set; }
}
public class Date_v0_1
{
private double DateStart;
private double DateEnd;
public DateTime DateTimeStart()
{
return CoreTime.JDToDateTime(DateStart);
}
public DateTime DateTimeEnd()
{
return CoreTime.JDToDateTime(DateEnd);
}
public void SetDateTimeStart(DateTime dt)
{
DateStart = CoreTime.DateTimeToJD(dt);
}
public void SetDateTimeEnd(DateTime dt)
{
DateEnd = CoreTime.DateTimeToJD(dt);
}
}
I've checked that the Json string, before writing, after writing and when read are the same. Here it is if it's helpful. If something more is needed I'll post it.
{
"Version": "v0_1",
"Date": "2020-01-08T10:49:04.6992512+01:00",
"Lista_v0_1": [
{
"ID": 2,
"Title": "Abadi gaztelua",
"Description": "",
"Rating": 0,
"Reminder": false,
"Toast": false,
"Geopoint": {
"Position": {
"Latitude": 43.380954170570355,
"Longitude": -1.7520569699123154,
"Altitude": 87.749645113013685
},
"AltitudeReferenceSystem": 2,
"GeoshapeType": 0,
"SpatialReferenceId": 4326
},
"Landmark": null,
"TimeZone": {
"Id": "Romance Standard Time",
"DisplayName": "(UTC+01:00) Brussels, Copenhagen, Madrid, Paris",
"StandardName": "Romance Standard Time",
"DaylightName": "Romance Daylight Time",
"BaseUtcOffset": "01:00:00",
"SupportsDaylightSavingTime": true
},
"MainTag": "Monument",
"Version": 0,
"Landmarks": {
"freeLandmark": false,
"Title": null,
"Astro": 0
},
"Images": [],
"URLs": [],
"Tags": [],
"Dates": [],
"Downloaded": false,
"Exportable": true
}
]
}
I think your class is not proper. I tried your code and it's working.
string filepath = "../../../json.txt";
var test = File.ReadAllText(filepath);
Example myDB = JsonConvert.DeserializeObject<Example>(test);
pl check your following class as per your json
public class Position
{
public double Latitude { get; set; }
public double Longitude { get; set; }
public double Altitude { get; set; }
}
public class Geopoint
{
public Position Position { get; set; }
public int AltitudeReferenceSystem { get; set; }
public int GeoshapeType { get; set; }
public int SpatialReferenceId { get; set; }
}
public class TimeZone
{
public string Id { get; set; }
public string DisplayName { get; set; }
public string StandardName { get; set; }
public string DaylightName { get; set; }
public string BaseUtcOffset { get; set; }
public bool SupportsDaylightSavingTime { get; set; }
}
public class Landmarks
{
public bool freeLandmark { get; set; }
public object Title { get; set; }
public int Astro { get; set; }
}
public class ListaV01
{
public int ID { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public int Rating { get; set; }
public bool Reminder { get; set; }
public bool Toast { get; set; }
public Geopoint Geopoint { get; set; }
public object Landmark { get; set; }
public TimeZone TimeZone { get; set; }
public string MainTag { get; set; }
public int Version { get; set; }
public Landmarks Landmarks { get; set; }
public IList<object> Images { get; set; }
public IList<object> URLs { get; set; }
public IList<object> Tags { get; set; }
public IList<object> Dates { get; set; }
public bool Downloaded { get; set; }
public bool Exportable { get; set; }
}
public class MyDB
{
public string Version { get; set; }
public DateTime Date { get; set; }
public IList<ListaV01> Lista_v0_1 { get; set; }
}
I have json string like this:
{
"data": [
{
"id": 1,
"name": "Bitcoin",
"symbol": "BTC",
...
"quote": {
"USD": {
"price": 9283.92,
"volume_24h": 7155680000,
"percent_change_1h": -0.152774,
"percent_change_24h": 0.518894,
"market_cap": 158055024432,
"last_updated": "2018-08-09T22:53:32.000Z"
},
"BTC": {
"price": 1,
"volume_24h": 772012,
"percent_change_1h": 0,
"percent_change_24h": 0,
"percent_change_7d": 0,
"market_cap": 17024600,
"last_updated": "2018-08-09T22:53:32.000Z"
}
}
},
// objects like previous from which i need the data
],
"status": {
"timestamp": "2018-06-02T22:51:28.209Z",
...
}
}
How do I deserialize it into models like this:
public class MyModel
{
public string Name { get; set; }
public string Symbol { get; set; }
public string Price { get; set; }
public double Percent_change_1h { get; set; }
public double Percent_change_24h { get; set; }
public long Market_cap { get; set; }
public DateTime Last_updated { get; set; }
}
The field names in the model are the same as the key names in json string.
I'm new to C# and I couldn't find any helpful information about my question, especially because of this specific json string structure.
I'll be glad if you direct me any good links about this.
The model seems to be something like this.
public class Model
{
public List<Datum> data { get; set; }
public Status status { get; set; }
}
public class Status
{
public DateTime timestamp { get; set; }
}
public class Datum
{
public int id { get; set; }
public string name { get; set; }
public string symbol { get; set; }
public Quote quote { get; set; }
}
public class Quote
{
public USD USD { get; set; }
public BTC BTC { get; set; }
}
public class BTC
{
public int price { get; set; }
public int volume_24h { get; set; }
public int percent_change_1h { get; set; }
public int percent_change_24h { get; set; }
public int percent_change_7d { get; set; }
public int market_cap { get; set; }
public DateTime last_updated { get; set; }
}
public class USD
{
public double price { get; set; }
public object volume_24h { get; set; }
public double percent_change_1h { get; set; }
public double percent_change_24h { get; set; }
public object market_cap { get; set; }
public DateTime last_updated { get; set; }
}
You can also try creating model on (http://json2csharp.com/) by copying your valid json string.
Please let me know if this helps
Bottom line: You can (manually), but that's probably not what you're looking for.
Reason: Your model doesn't match the JSON structure, hence "manual"
You can use readily available tools in either Visual Studio or VS Code to help you with creating the proper model (e.g. Paste JSON As Code)
Once you get the "proper" model/s ready, go over JSON documentation for (de)serializing.
I had to fix some syntax errors on your json, so fixed version is following:
{
"data": [
{
"id": 1,
"name": "Bitcoin",
"symbol": "BTC",
"quote": {
"USD": {
"price": 9283.92,
"volume_24h": 7155680000,
"percent_change_1h": -0.152774,
"percent_change_24h": 0.518894,
"market_cap": 158055024432,
"last_updated": "2018-08-09T22:53:32.000Z"
},
"BTC": {
"price": 1,
"volume_24h": 772012,
"percent_change_1h": 0,
"percent_change_24h": 0,
"percent_change_7d": 0,
"market_cap": 17024600,
"last_updated": "2018-08-09T22:53:32.000Z"
}
}
}
],
"status": {
"timestamp": "2018-06-02T22:51:28.209Z"
}
}
Here is C# model classes matching with previous json:
public class Rootobject
{
public Datum[] data { get; set; }
public Status status { get; set; }
}
public class Status
{
public DateTime timestamp { get; set; }
}
public class Datum
{
public int id { get; set; }
public string name { get; set; }
public string symbol { get; set; }
public Quote quote { get; set; }
}
public class Quote
{
public USD USD { get; set; }
public BTC BTC { get; set; }
}
public class USD
{
public float price { get; set; }
public long volume_24h { get; set; }
public float percent_change_1h { get; set; }
public float percent_change_24h { get; set; }
public long market_cap { get; set; }
public DateTime last_updated { get; set; }
}
public class BTC
{
public int price { get; set; }
public int volume_24h { get; set; }
public int percent_change_1h { get; set; }
public int percent_change_24h { get; set; }
public int percent_change_7d { get; set; }
public int market_cap { get; set; }
public DateTime last_updated { get; set; }
}
Here is code snippet which you can use when deserializing your json. This snippet uses Json.NET-library.
var obj = JsonConvert.DeserializeObject<Rootobject>(File.ReadAllText("object.json"));
I have some JSON which I'd like to deserialize into Objects.
I'm trying to do it like this, but I only get some of the JSON deserialized and the vin and vout JSON are either 0s or nulls. What am I doing wrong?
Transaction t = JsonConvert.DeserializeObject<RPCResponse<Transaction>>(json).result;
JSON:
{
"result": {
"hex": "01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0704ffff001d0104ffffffff0100f2052a0100000043410496b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52da7589379515d4e0a604f8141781e62294721166bf621e73a82cbf2342c858eeac00000000",
"txid": "0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098",
"hash": "0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098",
"size": 134,
"version": 1,
"locktime": 0,
"vin": [
{
"coinbase": "04ffff001d0104",
"sequence": 4294967295
}
],
"vout": [
{
"value": 50.00000000,
"n": 0,
"scriptPubKey": {
"asm": "0496b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52da7589379515d4e0a604f8141781e62294721166bf621e73a82cbf2342c858ee OP_CHECKSIG",
"hex": "410496b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52da7589379515d4e0a604f8141781e62294721166bf621e73a82cbf2342c858eeac",
"reqSigs": 1,
"type": "pubkey",
"addresses": [
"12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX"
]
}
}
],
"blockhash": "00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048",
"confirmations": 563356,
"time": 1231469665,
"blocktime": 1231469665
},
"error": null,
"id": "getrawtransaction"
}
The output in the debugger looks like this:
My Classes look like this:
public class RPCResponse<T>
{
public T result { get; set; }
public string error { get; set; }
public string id { get; set; }
}
public class Transaction
{
public string TxId { get; set; }
public long Size { get; set; }
public long Version { get; set; }
public long LockTime { get; set; }
public TransactionInputCoinbase[] Vin { get; set; }
public TransactionOutput[] Vout { get; set; }
public string BlockHash { get; set; }
public long Time { get; set; }
}
public class TransactionInputCoinbase
{
string Coinbase { get; set; }
string Sequence { get; set; }
}
public class TransactionOutput
{
decimal Value { get; set; }
int N { get; set; }
ScriptPubKey ScriptPubKey { get; set; }
}
public class ScriptPubKey
{
string Asm { get; set; }
string Hex { get; set; }
long ReqSigs { get; set; }
string Type { get; set; }
string[] Addresses { get; set; }
}
The lack of public properties on the said objects
For example
public class TransactionInputCoinbase
{
string Coinbase { get; set; } //<--NOT PUBLIC
string Sequence { get; set; } //<--NOT PUBLIC
}
means those properties will never be set when initialized via deserialization.
The same goes for TransactionOutput and ScriptPubKey based on the code originally provided.
Using the Json.NET library, I'm having trouble deserializing some json returned as an array. The json is an array containing some paging information as an object and array of country objects. Here's a sample of the returned json:
[
{
"page": 1,
"pages": 6,
"per_page": "50",
"total": 262
},
[
{
"id": "ABW",
"iso2Code": "AW",
"name": "Aruba",
"region": {
"id": "LCN",
"value": "Latin America & Caribbean (all income levels)"
},
"adminregion": {
"id": "",
"value": ""
},
"incomeLevel": {
"id": "NOC",
"value": "High income: nonOECD"
},
"lendingType": {
"id": "LNX",
"value": "Not classified"
},
"capitalCity": "Oranjestad",
"longitude": "-70.0167",
"latitude": "12.5167"
}
]
]
I am attempting to deserialize to the following types:
class CountriesQueryResults
{
public PagingInfo PageInfo { get; set; }
public List<CountryInfo> Countries { get; set; }
}
class PagingInfo
{
public int Page { get; set; }
public int Pages { get; set; }
[JsonProperty("per_page")]
public int ResultsPerPage { get; set; }
public int Total { get; set; }
}
class CountryInfo
{
public string Id { get; set; }
public string Iso2Code { get; set; }
public string Name { get; set; }
public string Longitude { get; set; }
public string Latitude { get; set; }
public string CapitalCity { get; set; }
public CompactIdentifier Region { get; set; }
public CompactIdentifier AdminRegion { get; set; }
public CompactIdentifier IncomeLevel { get; set; }
public CompactIdentifier LendingType { get; set; }
}
class CompactIdentifier
{
public string Id { get; set; }
public string Value { get; set; }
}
I am calling DeserializeObject as so:
var data = JsonConvert.DeserializeObject<List<CountriesQueryResults>>(response);
I am getting the following error:
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'CountriesQueryResults' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
I have been trying to get the answer from the documentation but I can't seem to figure it out. Any help would be appreciated.
Since your json is like this [ {....} , [{....}] ], You can only deserialize it to an object array(Where first item is an object and second, another array).
This simplest way I think to convert it to a c# object is:
var jArr = JArray.Parse(jsonstr);
var pageInfo = jArr[0].ToObject<PagingInfo>();
var countryInfos = jArr[1].ToObject<List<CountryInfo>>();
Class definitions would be:
public class PagingInfo
{
public int page { get; set; }
public int pages { get; set; }
[JsonProperty("per_page")]
public int ResultsPerPage { get; set; }
public int total { get; set; }
}
public class Region
{
public string id { get; set; }
public string value { get; set; }
}
public class AdminRegion
{
public string id { get; set; }
public string value { get; set; }
}
public class IncomeLevel
{
public string id { get; set; }
public string value { get; set; }
}
public class LendingType
{
public string id { get; set; }
public string value { get; set; }
}
public class CountryInfo
{
public string id { get; set; }
public string iso2Code { get; set; }
public string name { get; set; }
public Region region { get; set; }
public AdminRegion adminregion { get; set; }
public IncomeLevel incomeLevel { get; set; }
public LendingType lendingType { get; set; }
public string capitalCity { get; set; }
public string longitude { get; set; }
public string latitude { get; set; }
}
PS: You can change the property names' first char to Uppercase if you want. I used http://json2csharp.com/ to automatically generate those classes.