JSON to a JSON array Cannot deserialize the current JSON object - json

I'm using Json.Net to DeserializeObject Json data into object/collection (list)
I'm not sure what I'm doing wrong and I have tried this:
List<LanguageObject> lang = JsonConvert.DeserializeObject<List<LanguageObject>>(jsonData);
and tried this:
LanguageObject.Results lang = JsonConvert.DeserializeObject<LanguageObject.Results>(jsonData);
I'm getting this error:
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[LanguageObject]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path 'meta', line 1, position 8.
here my Json:
{"meta":
{
"status":200,
"message":[],
"resultSet":
{"id":"05"},
"pagination":
{
"count":2,
"pageNum":1,
"pageSize":2,
"totalPages":1,
"sort":"ordinal",
"order":"Asc",
"currentUri":null,
"nextUri":null
}
},
"results":
{
"id":0,
"name":
"Language",
"DName":null,
"qvalue":"language",
"language":null,
"mUsageCount":null,
"Ordinal":0,
"items":
[
{
"id":0,
"name":"English",
"DName":"English",
"qvalue":null,
"language":null,
"mUsageCount":null,
"Ordinal":0,
"items":[]
},
{
"id":0,
"name":"Spanish",
"DName":"Spanish;",
"qvalue":null,
"language":null,
"mUsageCount":null,
"Ordinal":0,"
items":[]
}
]
}}
LanguageObject.cs
Public class LanguageObject
{
public class ResultSet
{
public string id { get; set; }
}
public class Pagination
{
public int count { get; set; }
public int pageNum { get; set; }
public int pageSize { get; set; }
public int totalPages { get; set; }
public string sort { get; set; }
public string order { get; set; }
public object currentUri { get; set; }
public object nextUri { get; set; }
}
public class Meta
{
public int status { get; set; }
public List<object> message { get; set; }
public ResultSet resultSet { get; set; }
public Pagination pagination { get; set; }
}
public class Item
{
public int id { get; set; }
public string name { get; set; }
public string DName { get; set; }
public object qvalue { get; set; }
public object language { get; set; }
public object mUsageCount { get; set; }
public int Ordinal { get; set; }
public List<object> items { get; set; }
public List<object> __invalid_name__
items { get; set; }
}
public class Results
{
public int id { get; set; }
public string name { get; set; }
public object DName { get; set; }
public string qvalue { get; set; }
public object language { get; set; }
public object mUsageCount { get; set; }
public int Ordinal { get; set; }
public List<Item> items { get; set; }
}
public class RootObject
{
public Meta meta { get; set; }
public Results results { get; set; }
}
}

I don't see a LanguageObject class defined in your question. I do see a RootObject class, however. From what you have posted, you need to deserialize like this:
RootObject root = JsonConvert.DeserializeObject<RootObject>(jsonData);
Then you can get the Results from the RootObject:
Results lang = root.Results;

Related

How to deserialize multiple objects in Xamarin Forms? [duplicate]

This question already has answers here:
How to Deserialize JSON data? C#
(5 answers)
Closed 3 years ago.
I have two different class using that I am sending json response through API to another application, in short I am getting following json response :
{
"lmob_Forms":[
{
"Fields":"Certificate Name",
"Validation":"R",
"TabIndex":"1",
"FieldType":"DropdownList",
"DateFormate":"",
"Details":null
},
],
"drop_Salutation":[
{
"id":1,
"idvalue":"Kumari"
},
{
"id":2,
"idvalue":"Mrs"
},
{
"id":3,
"idvalue":"Ms"
}
]
}
I have searched regarding the same but not able to solve this problem, Need help, Thanks In Advance :)
First of all create a model of your json object.
public class LmobForm
{
public string Fields { get; set; }
public string Validation { get; set; }
public string TabIndex { get; set; }
public string FieldType { get; set; }
public string DateFormate { get; set; }
public object Details { get; set; }
}
public class DropSalutation
{
public int id { get; set; }
public string idvalue { get; set; }
}
public class RootObject
{
public List<LmobForm> lmob_Forms { get; set; }
public List<DropSalutation> drop_Salutation { get; set; }
}
Now, simply use Newtonsoft.Json to deserialize your JSON object as:
var myObj = JsonConvert.DeserializeObject<RootObject>(jsonString);
using json2csharp
public class LmobForm
{
public string Fields { get; set; }
public string Validation { get; set; }
public string TabIndex { get; set; }
public string FieldType { get; set; }
public string DateFormate { get; set; }
public object Details { get; set; }
}
public class DropSalutation
{
public int id { get; set; }
public string idvalue { get; set; }
}
public class RootObject
{
public List<LmobForm> lmob_Forms { get; set; }
public List<DropSalutation> drop_Salutation { get; set; }
}

Extracting JSON data using Newtonsoft in xamarin.android

Hello I am getting JSON data from server and i want to extract that JSON in Xamarin. How can i parse that JSON using NewTonSoft
below is the JSON responce i receive
[
{
"Id": 5,
"AlbumKey": "2REC2ZDSFK",
"ZipFillPath": "aaaa#gmail.com\\2REC2ZDSFK",
"NoOfPages": 3,
"EmailID": "aaaa#gmail.com"
}
]
This should be your Model
public class RootObject
{
public int Id { get; set; }
public string AlbumKey { get; set; }
public string ZipFillPath { get; set; }
public int NoOfPages { get; set; }
public string EmailID { get; set; }
}
Then
RootObject myObj = JsonConvert.DeserializeObject<RootObject>(json);
If your json is a List of objects, something like
List<RootObject> myListObj = JsonConvert.DeserializeObject<List<RootObject>>(json);
public class yourClass
{
public int Id { get; set; }
public string AlbumKey { get; set; }
public string ZipFillPath { get; set; }
public int NoOfPages { get; set; }
public string EmailID { get; set; }
}
Considering this as your model class you can
var responseText= JsonConvert.DeserializeObject<yourClass>(jsonResponse);
Then depending on if its a list or a not you can get the data from it
In case you are unable to find the class what you can do is check if the namespace of your current class and that class is the same.

Deserialize Json Array Newtonsoft.Json

I have the following JSON
[
{
"id":"656332",
"t":"INTU",
"e":"NASDAQ",
"l":"108.35",
"l_fix":"108.35",
"l_cur":"108.35",
"s":"2",
"ltt":"4:00PM EST",
"lt":"Nov 8, 4:00PM EST",
"lt_dts":"2016-11-08T16:00:01Z",
"c":"+0.45",
"c_fix":"0.45",
"cp":"0.42",
"cp_fix":"0.42",
"ccol":"chg",
"pcls_fix":"107.9",
"el":"108.43",
"el_fix":"108.43",
"el_cur":"108.43",
"elt":"Nov 8, 4:15PM EST",
"ec":"+0.08",
"ec_fix":"0.08",
"ecp":"0.08",
"ecp_fix":"0.08",
"eccol":"chg",
"div":"0.34",
"yld":"1.26"
},
{
"id":"13756934",
"t":".IXIC",
"e":"INDEXNASDAQ",
"l":"5,193.49",
"l_fix":"5193.49",
"l_cur":"5,193.49",
"s":"0",
"ltt":"4:16PM EST",
"lt":"Nov 8, 4:16PM EST",
"lt_dts":"2016-11-08T16:16:29Z",
"c":"+27.32",
"c_fix":"27.32",
"cp":"0.53",
"cp_fix":"0.53",
"ccol":"chg",
"pcls_fix":"5166.1729"
}
]
I'm trying to deserialize this array and use but I keep getting the following error:
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'StockTicker.home+Class1' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
Here is my class:
public class Rootobject
{
public Class1[] Property1 { get; set; }
}
public class Class1
{
public string id { get; set; }
public string t { get; set; }
public string e { get; set; }
public string l { get; set; }
public string l_fix { get; set; }
public string l_cur { get; set; }
public string s { get; set; }
public string ltt { get; set; }
public string lt { get; set; }
public DateTime lt_dts { get; set; }
public string c { get; set; }
public string c_fix { get; set; }
public string cp { get; set; }
public string cp_fix { get; set; }
public string ccol { get; set; }
public string pcls_fix { get; set; }
public string el { get; set; }
public string el_fix { get; set; }
public string el_cur { get; set; }
public string elt { get; set; }
public string ec { get; set; }
public string ec_fix { get; set; }
public string ecp { get; set; }
public string ecp_fix { get; set; }
public string eccol { get; set; }
public string div { get; set; }
public string yld { get; set; }
}
Here is what I'm trying to do:
var jsonObject1 = JsonConvert.DeserializeObject<Rootobject>(json);
var blah = jsonObject1.Property1[0].c;
I have no idea what to do at this point.
As the exception states, your outermost JSON container is an array -- a comma-delimited sequence of values surrounded by [ and ]. As such, it needs to be deserialized into a collection of some sort, as is explained in the docs. Thus you want to do:
var items = JsonConvert.DeserializeObject<Class1 []>(json);
var blah = items[0].c;
Sample fiddle.

Parse Json to List

I want to parse a json to List how can we do that. I have tried the following code but it didnt worked
Dictionary<string, object> pGateways=(Dictionary<string,object>)Json.JsonParser.FromJson(jsonString);
List<object> creditOptions = new List<object>();
creditOptions = (List<object>)pGateways;
And after getting it int list i want to loop through it
Here is my sample json
{
"MessageCode": "CS2009",
"Status": "Y",
"ErrorCode": "0",
"ErrorDescription": "Success",
"account":
{
"card":
[
{
"cardend": "asd",
"token": "aads",
"cardstart": "asdad",
"accounttype": "asda",
"cardnetwork": "as",
"issuer": "asd",
"customername": "a",
"expdate": "04/2018"
},
{
"cardend": "asda",
"token":"adssadsa",
"cardstart": "asd",
"accounttype": "asd",
"cardnetwork": "asd",
"issuer": "asda",
"customername": "asd",
"expdate": "03/2016"
}
],
"bank": []
}
}
The best option could be to use the JsonConvert in order to parse Json into a List.
Reference: JSON Parsing in Windows Phone
You can use Json.Net.
To install Json.NET use NugetGallery : Json.net Nugets Gallery
And you can use json2Csharp.com for generate c# classes from json
The JSON string you posted is not suitable for straight-forward deserialization to List. The easiest thing to do is use the online JSON 2 CSharp tool to generate classes and deserialize the json string to it. Here is an example of the generated classes:
public class Card
{
public string cardend { get; set; }
public string token { get; set; }
public string cardstart { get; set; }
public string accounttype { get; set; }
public string cardnetwork { get; set; }
public string issuer { get; set; }
public string customername { get; set; }
public string expdate { get; set; }
}
public class Account
{
public List<Card> card { get; set; }
public List<object> bank { get; set; }
}
public class RootObject
{
public string MessageCode { get; set; }
public string Status { get; set; }
public string ErrorCode { get; set; }
public string ErrorDescription { get; set; }
public Account account { get; set; }
}
And here is the logic for deserialization:
var root = JsonConvert.DeserializeObject<RootObject>(jsonStr);
where the jsonStr variable holds the json string you posted.
You need to use json2csharp tool to generate classes and deserialize the JSON string to list.
Here is the Classes generated from your JSON string.
public class Card
{
public string cardend { get; set; }
public string token { get; set; }
public string cardstart { get; set; }
public string accounttype { get; set; }
public string cardnetwork { get; set; }
public string issuer { get; set; }
public string customername { get; set; }
public string expdate { get; set; }
}
public class Account
{
public List<Card> card { get; set; }
public List<object> bank { get; set; }
}
public class RootObject
{
public string MessageCode { get; set; }
public string Status { get; set; }
public string ErrorCode { get; set; }
public string ErrorDescription { get; set; }
public Account account { get; set; }
}
and Deserialize your JSON object using JsonConvert,
Suppose e.result is your JSON string then
var rootObject = JsonConvert.DeserializeObject<RootObject>(e.Result);
foreach (var blog in rootObject.Card)
{
//access your data like this- `blog.cardend;` or `blog.token;`
}

How can I deserialization JSON in Windows Phone?

The first JSON is look like this
The second JSON is look like this
How can I deserialize them? I have been follow this example but it's not working.
Here's my code.
protected override void OnNavigatedTo(NavigationEventArgs e)
{
var w = new WebClient();
Observable
.FromEvent<DownloadStringCompletedEventArgs>(w, "DownloadStringCompleted")
.Subscribe(r =>
{
var deserialized =
JsonConvert.DeserializeObject<List<Place>>(r.EventArgs.Result);
PlaceList.ItemsSource = deserialized;
});
w.DownloadStringAsync(
new Uri("http://mobiilivantaa.lightscreenmedia.com/api/place"));
//For 2nd JSON
//w.DownloadStringAsync(
//new Uri("http://mobiilivantaa.lightscreenmedia.com/api/place/243"));
}
These are classes for 1st JSON.
public class Place
{
public string id { get; set; }
public string title { get; set; }
public string latitude { get; set; }
public string longitude { get; set; }
public string www { get; set; }
}
public class RootObjectJSON1
{
public List<Place> Places { get; set; }
}
These are classes for JSON2
public class Address
{
public string street { get; set; }
public string postal_code { get; set; }
public string post_office { get; set; }
}
public class Image
{
public string id { get; set; }
public string filename { get; set; }
public string path { get; set; }
}
public class RootObjectJSON2
{
public string id { get; set; }
public string title { get; set; }
public string description { get; set; }
public string latitude { get; set; }
public string longitude { get; set; }
public string www { get; set; }
public string phone { get; set; }
public string email { get; set; }
public string contact_person { get; set; }
public Address address { get; set; }
public List<Image> images { get; set; }
}
it looks that you should be deserializing object RootObjectJSON1 or RootObjectJSON2, e.g.:
var deserialized = JsonConvert.DeserializeObject<RootObjectJSON1>(r.EventArgs.Result);
Also it seems that collection Places should be with lowercase p at beginning or you need to tell Json.NET that this property should be deserialized with different name, e.g.:
[JsonProperty(PropertyName="places")]
public List<Place> Places { get; set; }
Generally I tend to use arrays for deserialization (from my experience works better) so I'll suggest to rewrite it to this:
public class RootObjectJSON1
{
public Place[] places { get; set; }
}
There is very good tool named json2csharp available at http://json2csharp.com/ - just put sample of JSON there and it will spit out classes in C# (doesn't detect DateTime so you might need to change that).