I need to convert data from Json(String) to an object to use its properties. I am keep having a null value.
I checked the Json file and my classes and I don't see the possible reason for this problem. Can you please help me?`
This a part from the Json file:
{
"global": {
"#context": "http://environment.data.gov.uk/flood-monitoring/meta/context.jsonld",
"meta": {
"publisher": "Environment Agency",
"licence": "http://www.nationalarchives.gov.uk/doc/open-government-licence/version/3/",
"documentation": "http://environment.data.gov.uk/flood-monitoring/doc/reference",
"version": "0.6.1",
"comment": "Status: Beta service",
"hasFormat": [
"http://environment.data.gov.uk/flood-monitoring/id/stations.csv"
]
},
"items": [
{
"#id": "http://environment.data.gov.uk/flood-monitoring/id/stations/1029TH",
"RLOIid": "7041",
"catchmentName": "Cotswolds",
"dateOpened": "1994-01-01",
"easting": 417990,
"label": "Bourton Dickler",
"lat": 51.874767,
"long": -1.740083,
"measures": [
{
"#id": "http://environment.data.gov.uk/flood-monitoring/id/measures/1029TH-level-downstage-i-15_min-mASD",
"parameter": "level",
"parameterName": "Water Level",
"period": 900,
"qualifier": "Downstream Stage",
"unitName": "mASD"
},
{
"#id": "http://environment.data.gov.uk/flood-monitoring/id/measures/1029TH-level-stage-i-15_min-mASD",
"parameter": "level",
"parameterName": "Water Level",
"period": 900,
"qualifier": "Stage",
"unitName": "mASD"
}
],
"northing": 219610,
"notation": "1029TH",
"riverName": "Dikler",
"stageScale": "http://environment.data.gov.uk/flood-monitoring/id/stations/1029TH/stageScale",
"stationReference": "1029TH",
"town": "Little Rissington",
"wiskiID": "1029TH"
},
{
"#id": "http://environment.data.gov.uk/flood-monitoring/id/stations/E2043",
"RLOIid": "6022",
"catchmentName": "Welland",
"dateOpened": "1992-01-01",
"datumOffset": 2,
"easting": 528000,
"label": "Surfleet Sluice",
"lat": 52.845991,
"long": -0.100848,
"measures": [
{
"#id": "http://environment.data.gov.uk/flood-monitoring/id/measures/E2043-level-stage-i-15_min-mASD",
"parameter": "level",
"parameterName": "Water Level",
"period": 900,
"qualifier": "Stage",
"unitName": "mASD"
}
],
"northing": 329300,
"notation": "E2043",
"riverName": "River Glen",
"stageScale": "http://environment.data.gov.uk/flood-monitoring/id/stations/E2043/stageScale",
"stationReference": "E2043",
"town": "Surfleet Seas End",
"wiskiID": "L31004"
},
These are the classes:
namespace TechnicalTestZK
{
class Program
{
static void Main(string[] args)
{
var json = File.ReadAllText("C:/Users/Zied/Desktop/Json.txt");
var ab = JsonConvert.DeserializeObject<Global>(json);
Console.WriteLine(ab.Items[0].RiverName);
//Console.WriteLine(GET("http://environment.data.gov.uk/flood-monitoring/id/floods"));
Console.ReadLine();
}
static string GET(string url)
{
System.Net.HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
try
{
WebResponse response = request.GetResponse();
using (Stream responseStream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
return reader.ReadToEnd();
}
}
catch (WebException ex)
{
WebResponse errorResponse = ex.Response;
using (Stream responseStream = errorResponse.GetResponseStream())
{
StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
String errorText = reader.ReadToEnd();
// log errorText
}
throw;
}
}
}
}
using Newtonsoft.Json;
namespace TechnicalTestZK
{
public class Meta
{
[JsonProperty("publisher")]
public string Publisher { get; set; }
[JsonProperty("licence")]
public string Licence { get; set; }
[JsonProperty("documentation")]
public string Documentation { get; set; }
[JsonProperty("version")]
public string Version { get; set; }
[JsonProperty("comment")]
public string Comment { get; set; }
[JsonProperty("hasFormat")]
public string[] HasFormat { get; set; }
}
}
using Newtonsoft.Json;
namespace TechnicalTestZK
{
public class Measure
{
[JsonProperty("#id")]
public string Id { get; set; }
[JsonProperty("parameter")]
public string Parameter { get; set; }
[JsonProperty("parameterName")]
public string ParameterName { get; set; }
[JsonProperty("period")]
public int Period { get; set; }
[JsonProperty("qualifier")]
public string Qualifier { get; set; }
[JsonProperty("unitName")]
public string UnitName { get; set; }
}
}
using Newtonsoft.Json;
namespace TechnicalTestZK
{
public class Item
{
[JsonProperty("#id")]
public string Id { get; set; }
[JsonProperty("RLOIid")]
public string RLOIid { get; set; }
[JsonProperty("catchmentName")]
public string CatchmentName { get; set; }
[JsonProperty("dateOpened")]
public string DateOpened { get; set; }
[JsonProperty("easting")]
public int Easting { get; set; }
[JsonProperty("label")]
public string Label { get; set; }
[JsonProperty("lat")]
public string Lat { get; set; }
[JsonProperty("long")]
public string Long { get; set; }
[JsonProperty("measures")]
public Measure[] Measures { get; set; }
[JsonProperty("northing")]
public int Northing { get; set; }
[JsonProperty("notation")]
public string Notation { get; set; }
[JsonProperty("riverName")]
public string RiverName { get; set; }
[JsonProperty("stageScale")]
public string StageScale { get; set; }
[JsonProperty("stationReference")]
public string StationReference { get; set; }
[JsonProperty("town")]
public string Town { get; set; }
[JsonProperty("wiskiID")]
public string WiskiID { get; set; }
[JsonProperty("datumOffset")]
public float? DatumOffset { get; set; }
[JsonProperty("downstageScale")]
public string DownstageScale { get; set; }
}
}
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace TechnicalTestZK
{
internal class Global
{
[JsonProperty("#context")]
public string #context { get; set; }
[JsonProperty("meta")]
public Meta Meta { get; set; }
[JsonProperty("items")]
public Item[] Items { get; set; }
}
}
The Json file you are trying to deserialize is one "level" higher than Global, which is what you are trying to deserialize into. Instead, make a new class that looks something like this:
public class GlobalContainer
{
[JsonProperty("global")]
public Global global { get; set; }
}
And deserialize into that by calling JsonConvert.DeserializeObject<GlobalContainer>(json)
Or you can use a json 2 # parser like http://json2csharp.com ;)
Related
How can i deserialize a local json file to an object?
I'm trying to deserialize a local json file in xamarin but it just doesn't work i read many guides and watched some tutorials but none of them helped and most of them give the same code i'm using right now
My code:
public test()
{
InitializeComponent();
List<Rootobject> ob = new List<Rootobject>();
var assembly = IntrospectionExtensions.GetTypeInfo(typeof(test)).Assembly;
Stream stream = assembly.GetManifestResourceStream($"TunisiaPrayer.states.json");
string text = "";
using (var reader = new StreamReader(stream))
{
text = reader.ReadToEnd();
}
ob = JsonConvert.DeserializeObject<List<Rootobject>>(text);
//printing the json file to make sure it read it fully
//i get no error here
DisplayJson.Text = text;
//try catch to test if it was deserialized or not
try
{
//printing a property of ob in a label element to see if it works
DisplayData.Text = ob[2].Property1.data.gouvernorat.intituleAn;
}
catch (Exception ex)
{
DisplayData.Text = ex.Message;
}
}
RootObject Class:
namespace TunisiaPrayer.Models
{
public class Rootobject
{
public Class1 Property1 { get; set; }
}
public class Class1
{
public Data data { get; set; }
}
public class Data
{
public Gouvernorat gouvernorat { get; set; }
public Delegation[] delegation { get; set; }
}
public class Gouvernorat
{
public int id { get; set; }
public string intituleAr { get; set; }
public string intituleAn { get; set; }
}
public class Delegation
{
public int id { get; set; }
public string intituleAr { get; set; }
public string intituleAn { get; set; }
}
}
sample of states.json:
[{
"data": {
"gouvernorat": {
"id": 358,
"intituleAr": "اريانة",
"intituleAn": "Ariana"
},
"delegation": [{
"id": 631,
"intituleAr": "اريانة",
"intituleAn": "Ariana"
},
{
"id": 534,
"intituleAr": "التظامن",
"intituleAn": "Attadhamon"
},
{
"id": 532,
"intituleAr": "سكرة",
"intituleAn": "Soukra"
}
]
}
},
{
"data": {
"gouvernorat": {
"id": 362,
"intituleAr": "القصرين",
"intituleAn": "Kasserine"
},
"delegation": [{
"id": 579,
"intituleAr": "العيون",
"intituleAn": "El Ayoun"
},
{
"id": 576,
"intituleAr": "سبيبة",
"intituleAn": "Sbiba"
},
{
"id": 575,
"intituleAr": "سبيطلة",
"intituleAn": "Sbitla"
},
{
"id": 573,
"intituleAr": "تالة",
"intituleAn": "Tala"
}
]
}
}]
error: Object reference not set to an instance of an object
note that when i print ob.Count it gives me the correct length of the list but i can't access any data in it
you have to use Root[] , not just root, try this
var obj =JsonConvert.DeserializeObject< List<Root>>(text);
test
var displayData = obj[1].data.gouvernorat.intituleAn; // = Kasserine
classes
public class Root
{
public Data data { get; set; }
}
public class Data
{
public Gouvernorat gouvernorat { get; set; }
public List<Delegation> delegation { get; set; }
}
public class Gouvernorat
{
public int id { get; set; }
public string intituleAr { get; set; }
public string intituleAn { get; set; }
}
public class Delegation
{
public int id { get; set; }
public string intituleAr { get; set; }
public string intituleAn { get; set; }
}
I have built a service that is retrieving a sample file.
return await _httpClient.GetFromJsonAsync<BitcoinDetails>("https://localhost:44356/sample-data/jsonresult.json");
Example
"id": 1,
"name": "Bitcoin",
"symbol": "BTC",
"slug": "bitcoin",
"num_market_pairs": 9550,
"date_added": "2013-04-28T00:00:00.000Z",
"tags": [
"mineable",
"pow",
"sha-256",
"store-of-value",
"state-channels"
],
"max_supply": 21000000, -- or null if not set
"circulating_supply": 18555956,
"total_supply": 18555956,
"platform": null,
"cmc_rank": 1,
"last_updated": "2020-11-27T21:22:02.000Z",
"quote": {
"USD": {
"price": 17069.598651577406,
"volume_24h": 38571181876.87967,
"percent_change_1h": 1.46329039,
"percent_change_24h": 0.92405679,
"percent_change_7d": -8.25816318,
"market_cap": 316742721516.32965,
"last_updated": "2020-11-27T21:22:02.000Z"
}
}
}
Now I have a class that has the following properties
public class Datum
{
public int id { get; set; }
public string name { get; set; }
public string symbol { get; set; }
public string slug { get; set; }
public int num_market_pairs { get; set; }
public DateTime date_added { get; set; }
public List<string> tags { get; set; }
public long? max_supply { get; set; }
public double circulating_supply { get; set; }
public double total_supply { get; set; }
public Platform platform { get; set; }
public int cmc_rank { get; set; }
public DateTime last_updated { get; set; }
public Quote quote { get; set; }
}
Keeping in mind that there are many more records I am getting the following error
Unhandled exception rendering component: The JSON value could not be converted to System.Nullable`1[System.Int64]. Path: $.data[80].max_supply |
make dto nullable and then try as follows:
var response = client.GetAsync(apiUrl);
if (response.Result.IsSuccessStatusCode)
{
var data = response.Result.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<Datum>(data.Result);
return result;
}
I ended up using a proxy call however this code did work:
var response = await _httpClient.GetAsync("https://localhost:5001/proxy");
if (response.IsSuccessStatusCode)
{
var data = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<BitcoinDetails>(data);
return result;
}
else
{
throw new Exception("Failed to get data");
}
and added this to the class property
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
Tried to parse JSON object using JavaScriptSerializer().Deserialize but getting thrown a null error at foreach (var item in getRoute.results) where Route getRoute = new JavaScriptSerializer().Deserialize<Route>(strresulttest);
The thing is, when I debugged strresulttest by printing out its contents, I was able to see my expected JSON output as shown here https://codeshare.io/5vM6X4
Code to parse JSON:
public class Route
{
public List<GetRoute> results { get; set; }
}
public class GetRoute
{
public string status_message { get; set; }
public string viaRoute { get; set; }
public string subtitle { get; set; }
public int totalTime { get; set; }
public int totalDistance { get; set; }
}
private void GetInstructions()
{
//GET METHOD for route query
string strurltest = String.Format("https://developers.onemap.sg/privateapi/routingsvc/route?start="+
startLat+","+ startLon +"&end="+ destinationLat +","+ destinationLon+"&"+
"routeType="+ transportType + "&token="+token);
WebRequest requestObjGet = WebRequest.Create(strurltest);
requestObjGet.Method = "GET";
HttpWebResponse responseObjGet = null;
responseObjGet = (HttpWebResponse)requestObjGet.GetResponse();
string strresulttest = null;
using (Stream stream = responseObjGet.GetResponseStream())
{
StreamReader sr = new StreamReader(stream);
strresulttest = sr.ReadToEnd();
//reminder: remove after prod. GET is working.
System.Diagnostics.Debug.WriteLine(strresulttest);
sr.Close();
}
//display search recommendations
Route getRoute = new JavaScriptSerializer().Deserialize<Route>(strresulttest);
foreach (var item in getRoute.results)
{
//reminder: remove after prod.
System.Diagnostics.Debug.WriteLine("Route via: " + item.viaRoute + "\n");
System.Diagnostics.Debug.WriteLine("Description: " + item.subtitle + "\n");
}
}
Expected JSON output (shown when System.Diagnostics.Debug.WriteLine(strresulttest);):
{
"status_message": "Found route between points",
"route_geometry": "m{`G_cyxRaAmALMtAyAj#g#RIZG|#KTC??RCxBQ\\KAOa#sDAQWwBM_AUmBEa#Ky#_#kDKoAIgAAs#Ce#?M|#_#PINUFk#Ik#e#aCu#wBeBoD_A}AmBqC{#iA_AeAyAqA{LmKmAu#k#g#y#Jk#r#k#r#GP#PFJhAt#oEnGw#i#QTMPs#g#}AzBr#d#JQ",
"status": 0,
"route_instructions": [
[
"Head",
"SINARAN DRIVE",
56,
"1.320394,103.844478",
18,
"56m",
"North East",
"North",
"driving",
"Head Northeast On Sinaran Drive"
],
[
...
],
...
],
"route_name": [
"MOULMEIN ROAD",
"WHAMPOA ROAD"
],
"route_summary": {
"start_point": "SINARAN DRIVE",
"end_point": "WHAMPOA ROAD",
"total_time": 390,
"total_distance": 2675
},
"viaRoute": "JALAN BAHAGIA",
"subtitle": "Fastest route, assuming usual traffic",
"phyroute": {
"status_message": "Found route between points",
"route_geometry": "m{`G_cyxRaAmALMtAyAj#g#RIZG|#KTC??Ao#BcB?kCBIHK|Ay#M_AUmBEa#Ky#_#kDKoAIgAAs#Ce#?M?OBw#BsCEq#Q{#Qm#KQGMaCNM#iFXO?sCNo#LCa#QaICe#EI?EBQTi#Ha##c#CMGWOYOOgEgD{BeBuB}AoEnGw#i#QTMPs#g#}AzBr#d#JQ",
"status": 0,
"route_instructions": [
[
"Head",
"SINARAN DRIVE",
56,
"1.320394,103.844478",
18,
"56m",
"North East",
"North",
"driving",
"Head Northeast On Sinaran Drive"
],
[
...
],
...
],
"route_name": [
...
],
"route_summary": {
...
},
"viaRoute": "BALESTIER ROAD",
"subtitle": "Shortest distance"
}
}
You are getting a null in results because the class structure that you are deserializing into does not match your JSON. The JSON you linked in your question corresponds to this class structure instead:
public class Route
{
public string status_message { get; set; }
public string route_geometry { get; set; }
public int status { get; set; }
public List<List<object>> route_instructions { get; set; }
public List<string> route_name { get; set; }
public RouteSummary route_summary { get; set; }
public string viaRoute { get; set; }
public string subtitle { get; set; }
public Phyroute phyroute { get; set; }
}
public class RouteSummary
{
public string start_point { get; set; }
public string end_point { get; set; }
public int total_time { get; set; }
public int total_distance { get; set; }
}
public class Phyroute
{
public string status_message { get; set; }
public string route_geometry { get; set; }
public int status { get; set; }
public List<List<object>> route_instructions { get; set; }
public List<string> route_name { get; set; }
public RouteSummary route_summary { get; set; }
public string viaRoute { get; set; }
public string subtitle { get; set; }
}
You can deserialize and get the viaRoute and subtitle like this:
Route route = new JavaScriptSerializer().Deserialize<Route>(strresulttest);
System.Diagnostics.Debug.WriteLine("Route via: " + route.viaRoute);
System.Diagnostics.Debug.WriteLine("Description: " + route.subtitle);
We have to do some changes in existing JSON response and it requires to modify existing c# code too. Till now I've tried but don't get the exact idea about the formatting as it is new for me.
Can any one please check and suggest the changes in below code?
Existing JSON message:
"hotel_room_types": {
"QUEEN": {
"code": "QUEEN",
"bed_type": {
"standard": [
5
],
"custom": []
},
"extra_bed_type": {
"standard": [
5
],
"custom": []
},
"accessibility": "compliant_with_local_laws_for_disabled",
"room_smoking_policy": "non_smoking"
}
}
In above JSON message we have to replace the "bed_type" and "extra_bed_type" with "bed_configurations" and "extra_bed_configurations" in below newly provided format by client:
"hotel_room_types": {
"QUEEN": {
"code": "QUEEN",
"bed_configurations": [
[{
"type": "standard",
"code": 3,
"count": 1
}],
[{
"type": "standard",
"code": 1,
"count": 2
}]
],
"extra_bed_configurations": [
[{
"type": "standard",
"code": 900302,
"count": 1
},
{
"type": "custom",
"name": "Rollaway with wheel locks and adjustable height",
"count": 1
}]
],
"accessibility": "compliant_with_local_laws_for_disabled",
"room_smoking_policy": "non_smoking"
}
}
Existing C# code to generate the JSON response message format:
public class HotelRoomType
{
public string code { get; set; }
public string name { get; set; }
public string description { get; set; }
public List<Photo> photos { get; set; }
public Amenities room_amenities { get; set; }
public string room_size { get; set; }
public string room_size_units { get; set; }
public BedType bed_type { get; set; }
public BedType extra_bed_type { get; set; }
public RoomViewType room_view_type { get; set; }
public string accessibility { get; set; }
public MaxOccupancy max_occupancy { get; set; }
public string room_smoking_policy { get; set; }
}
Below is the code to fill the data in required fields of JSON message its inside a method which contains lines of code so I get it separately:
HotelRoomType hotelrmtype = new HotelRoomType();
hotelrmtype.bed_type = Common.GetStandardBedTypeMappingID(rm.BedType);
if (rm.NumOfBed > 1)
hotelrmtype.extra_bed_type = hotelrmtype.bed_type; //same as bed type
hotelrmtypeDict.Add(rm.Code, hotelrmtype); //Binding Data into Dictionary.
GetStandardBedTypeMappingID() contains :
public static CRS.TripConnect.BedType GetStandardBedTypeMappingID(short code)
{
CRS.TripConnect.BedType tripConnectBedType = new CRS.TripConnect.BedType();
List<int> standardBedTypes = new List<int>();
List<object> customBedTypes = new List<object>();
tripConnectBedType.standard = standardBedTypes;
tripConnectBedType.custom = customBedTypes; //These is blank.
short id = 0;
switch (code)
{
case 10:
id = 3;
break;
case 20: // 20 Queen Q 5
id = 5;
break;
case 30: // 30 Double D 1
id = 1;
break;
case 40: // 40 Twin T 8
id = 8;
break;
}
standardBedTypes.Add(id);
return tripConnectBedType;
}
Update: With the help of #Sam Answer below I have modified the code:
public class HotelRoomType
{
//Added following properties
public List<List<Bed_Configurations>> bed_configurations { get; set; }
public List<List<Extra_Bed_Configurations>> extra_bed_configurations { get; set; }
}
Created two new classes as:
public class Bed_Configurations
{
public string type { get; set; }
public int code { get; set; }
public int count { get; set; }
}
public class Extra_Bed_Configurations
{
public string type { get; set; }
public int code { get; set; }
public int count { get; set; }
public string name { get; set; }
}
Now the question is: How to fill these two list?
I'm trying to achieve this like below but it is giving me conversion error.
Bed_Configurations bdConfig = new Bed_Configurations();
bdConfig.type = "Standard";
bdConfig.code = Common.GetStandardBedTypeMappingIDnew(rm.BedType);
bdConfig.count = rm.NumOfBed;
hotelrmtype.bed_configurations.Add(bdConfig);
Error Message:
Please Advise the changes in the above code so as to get the required JSON message. Appreciate your help!
public class RootClass
{
public HotelRoomType hotel_room_types { get; set; }
}
public class HotelRoomType
{
public BedModelAndDetails QUEEN { get;set; }
}
public class BedModelAndDetails
{
public string accessibility { get; set; }
public string room_smoking_policy { get; set; }
public string code { get; set; }
//public BedType bed_type { get; set; }
public object bed_type { get; set; }
//public BedType extra_bed_type { get; set; }
public object extra_bed_type { get; set; }
public List<List<BedConfiguration>> bed_configurations { get; set; }
public List<List<BedConfiguration>> extra_bed_configurations { get; set; }
}
public class BedType
{
public List<int> standard { get; set; }
public List<int> custom { get; set; }
}
public class BedConfiguration
{
public string type { get; set; }
public int code { get; set; }
public int count { get; set; }
}
[TestMethod]
public void ReplaceJson()
{
var inputJson1 = "{\"hotel_room_types\": {\"QUEEN\": {\"code\": \"QUEEN\", \"bed_type\": {\"standard\": [5],\"custom\": [] }, \"extra_bed_type\": { \"standard\": [5], \"custom\": [] },\"accessibility\": \"compliant_with_local_laws_for_disabled\", \"room_smoking_policy\": \"non_smoking\" } " +"}}";
var input1 = JsonConvert.DeserializeObject<RootClass>(inputJson1, new JsonSerializerSettings {NullValueHandling = NullValueHandling.Ignore});
var inputJson2 = "{\"hotel_room_types\": {\"QUEEN\": {\"code\": \"QUEEN\",\"bed_configurations\": [[{\"type\": \"standard\",\"code\": 3,\"count\": 1}],[{\"type\": \"standard\",\"code\": 1,\"count\": 2}]],\"extra_bed_configurations\": [[{\"type\": \"standard\",\"code\": 900302,\"count\": 1},{\"type\": \"custom\",\"name\": \"Rollaway with wheel locks and adjustable height\",\"count\": 1}]],\"accessibility\": \"compliant_with_local_laws_for_disabled\",\"room_smoking_policy\": \"non_smoking\"} }}";
var input2 = JsonConvert.DeserializeObject<RootClass>(inputJson2);
//var finalInput = new RootClass();
//finalInput.hotel_room_types = inputJson1
//input1.hotel_room_types.QUEEN.bed_configurations = input2.hotel_room_types.QUEEN.bed_configurations;
//input1.hotel_room_types.QUEEN.extra_bed_configurations = input2.hotel_room_types.QUEEN.extra_bed_configurations;
input1.hotel_room_types.QUEEN.bed_type = input2.hotel_room_types.QUEEN.bed_configurations;
input1.hotel_room_types.QUEEN.extra_bed_type = input2.hotel_room_types.QUEEN.extra_bed_configurations;
}
Does this help?
I have the simple JSON:
[
{
"new_as_cod": "0010955",
"as_nome": "NAME",
"as_cpf": "1212121212",
"as_email": "IM#UOL.COM.BR",
"as_cep": "88.025-200",
"igr_nome": "1\u00aa IGREJA BATISTA - FLORIANOPOLIS",
"id": "2781",
"valor": "50.00",
"pg_tipo_id": "CC",
"status": "Ativo",
"idstatus": "1"
}
]
... and a C# class generated from here:
public class RootObject
{
public string new_as_cod { get; set; }
public string as_nome { get; set; }
public string as_cpf { get; set; }
public string as_email { get; set; }
public string as_cep { get; set; }
public string igr_nome { get; set; }
public string id { get; set; }
public string valor { get; set; }
public string pg_tipo_id { get; set; }
public string status { get; set; }
public string idstatus { get; set; }
}
I have tried this:
RootObject data = JsonConvert.DeserializeObject<RootObject>(stringdate);
But I get the error:
How can I solve it?
[{ "new_as_cod": "0010955", "as_nome": "NAME", "as_cpf": "1212121212", "as_email": "IM#UOL.COM.BR", "as_cep": "88.025-200", "igr_nome": "1\u00aa IGREJA BATISTA - FLORIANOPOLIS", "id": "2781", "valor": "50.00", "pg_tipo_id": "CC", "status": "Ativo", "idstatus": "1" }]
If it has [] this is a collection.
Try this.
JsonConvert.DeserializeObject<List<RootObject>>(stringdate);
Yes, this JSON is a collection, so the variable needs to be list too.
List<RootObject> data = JsonConvert.DeserializeObject<List<RootObject>>(stringdate);