I have such scheme of document:
{
"_id" : ObjectId("4fbb728d80db260988580e05"),
"titleFull" : "Foo, Inc",
"titleShort" : "Foo",
"countries" : [
ObjectId("4fba04ef80db260988f8b607"),
ObjectId("4fba05f880db260988cd5cfd") ],
"type" : "company"
}
And such class in ASP.NET MVC 4 Web API project:
public class Company
{
[BsonRepresentation(BsonType.ObjectId)]
public String id { get; set; }
public String titleFull { get; set; }
public String titleShort { get; set; }
//[BsonRepresentation(BsonType.ObjectId)]
//public String[] countries { get; set; } — not working
public ObjectId[] countries { get; set; }
public String type { get; set; }
}
When I'm sending GET request on /api/countries I receive JSON document (It's mvc deserialization):
{
"id": "4fba097e80db2609886ce7f2",
"titleFull": "Foo, LLC",
"titleShort": "Foo",
"countries": [
{
"_increment": 16299527
"_machine": 8444710
"_pid": 2440
"_timestamp": 1337591023
},
{
"_increment": 13458685
"_machine": 8444710
"_pid": 2440
"_timestamp": 1337591288
}
],
"type": "company"
}
Is there a way to do JSON response like this:
{
"id": "4fba097e80db2609886ce7f2",
"titleFull": "Foo, LLC",
"titleShort": "Foo",
"countries": ["4fba04ef80db260988f8b607","4fba05f880db260988cd5cfd"],
"type": "company"
}
Note to future readers
Rober Stam wrote in google groups:
There is a bug in the deserialization code. In the case of an array the [BsonRepresentation] attribute is in fact applied to the items and not the array.
I've created a JIRA ticket for this:
https://jira.mongodb.org/browse/CSHARP-479
So if you have the same issue, please track this ticket.
In the response instead of use ObjectId for Countries use a Array of string and add the ids that you want to pass in the json response.
public string[] countries { get; set; }
if you use like this the response will be something like this in json
"countries": ["4fba04ef80db260988f8b607","4fba05f880db260988cd5cfd"],
You are using a string already in the id field.
Related
I need to retrieve the following JSON from my CosmosDb, but I think the C# code (which works for a simple JSON structure), needs help.
The response just shows
{ RolePermission[0] }
var response = this.client.CreateDocumentQuery<RolePermission>(
UriFactory.CreateDocumentCollectionUri("demo", "RoleMenus"), queryOptions)
.Where(f => f.RoleName == roleName).AsEnumerable().ToArray();
and the JSON is:
{
"id": "1",
"RoleName": "Admin",
"Menus": [
{
"Item": "Admin",
"Display": "Admin",
"Granted": true
},
{
"Item": "Users",
"Display": "Users",
"Granted": true
}
],
}
The Class defn, I want to get it into is:
public class RolePermission
{
[DisplayName("Role Name")]
public string RoleName { get; set; }
public List<Menus> Menus { get; set; }
}
public class Menus
{
public string Item { get; set; }
public string Display { get; set; }
public bool Granted { get; set; }
}
I just need the menus part of the JSON
Thanks
According to the data that you provided and the code , it should return the list you need "Menus"
with the below query
var response = this.client.CreateDocumentQuery<RolePermission>(UriFactory.CreateDocumentCollectionUri("demo", "RoleMenus"), queryOptions).Where(f => f.RoleName == roleName).ToList();
Make sure you have the correct data inserted with the field RoleName in the container.
do I always need to create a model for an incoming JSON and then deserialize the JSON to it or is there a way to loop through the JSON file, check specific properties and add additional information?
Here is why I ask:
In my project I have two classes "region" and "country". Inside "region" is the nested class "country"
enter code here
public class Region`enter code here`
{
[Key]
public int Id { get; set; }
public string Region { get; set; }
public Country Country { get; set; }
}
public class Country
{
[Key]
public int Id { get; set; }
[Required]
[MaxLength(255)]
public string Name { get; set; }
[Required]
[MaxLength(2)]
public string ISO3166Alpha2 { get; set; }
[Required]
[MaxLength(3)]
public string ISO3166Alpha3 { get; set; }
}
Now I have an incoming JSON which has the country and region information which i need but without Ids or any other required information.
So do i need to create a new model, deserialize the JSON and then use that new model to create my "region" and "country" model or is there a better way of doing this?
{
"regions": [
{
"country": "France",
"region": [
"Bordeaux",
"Burgundy",
"Beaujolais",
"Champagne",
"Loire",
"Alsace",
"Rhône",
"Provence",
"Languedoc-Roussillon",
"Jura"
]
},
{
"country": "Italy",
"region": [
"Piedmont",
"Barolo",
"Barbaresco",
"Tuscany",
"Veneto",
"Friuli-Venezia",
"Giulia",
"Abruzzo",
"Sicily",
"Lambrusco"
]
}
}
´´´
Thanks a lot :)
kindly find below the way to achieve that as stated by the docs. kindly visit this link https://www.newtonsoft.com/json/help/html/DeserializeAnonymousType.htm but however my solution is shown below.
var json1 = #"{
'regions': [
{
'country': 'France',
'region': [
'Bordeaux',
'Burgundy',
'Beaujolais',
'Champagne',
'Loire',
'Alsace',
'Rhône',
'Provence',
'Languedoc-Roussillon',
'Jura'
]
},
{
'country': 'Italy',
'region': [
'Piedmont',
'Barolo',
'Barbaresco',
'Tuscany',
'Veneto',
'Friuli-Venezia',
'Giulia',
'Abruzzo',
'Sicily',
'Lambrusco'
]
}]}";
var definition = new { regions = new[]{new {country = "" , region = new string[]{ } } }};
var customer1 = JsonConvert.DeserializeAnonymousType(json1, definition);
Console.WriteLine(customer1.regions);
foreach (var item in customer1.regions)
{
Console.WriteLine(item.country);
}
I just got my json response as a string.My json is given below,
"code": 0,
"message": "success",
"students": {
"details":{
"hjeke": {
"id": "257633000000070001",
"name": "hjeke",
"percentage": 36,
"type": "Good",
},
"Second": {
"id": "257633000000073001",
"name": "Second",
"percentage": 4,
"type": "bad",
}
}
}
Like hjeke and Second there are many key value pairs,how can i deserialize my json using Newtonsoft.json
Try to understand my solution in your previous question
How to deserialize json data in windows phone?
Your first JSON in that question was good and simple to use.
JSON, where field names are unique not convinient to deserialize. So, you got problems such as public class Hjeke and public class Second for each instance, when you use code generator.
Use JSON-structure with list of students:
"code": 0,
"message": "success",
"students": [
{
"id": "257633000000070001",
"name": "hjeke",
"percentage": 36,
"type": "Good",
},
{
"id": "257633000000073001",
"name": "Second",
"percentage": 4,
"type": "bad",
}]
is good and flexible structure. Using this, you don't need to parse not obvious fields like
"details":{
"hjeke": {
and so on.
And work with them using classes, from my previous answer. The main idea - you need list of objects. public List<StudentDetails> students. Then, all students objects deserialized in List, which is easy to use.
As everybody mentioned your json seems to be very unflexible, huh.
You can extract the data you are interested in.
So this is your model:
public class StudentDetails
{
public string id { get; set; }
public string name { get; set; }
public int percentage { get; set; }
public string type { get; set; }
}
And this is how you can extract it:
var jsonObj = JObject.Parse(str);
// get JSON result objects into a list
var results = jsonObj["students"]["details"].Children().Values();
// serialize JSON results into .NET objects
var details = new List<StudentDetails>();
foreach (JToken result in results)
{
var st = result.ToString();
var searchResult = JsonConvert.DeserializeObject<StudentDetails>(st);
details.Add(searchResult);
}
I'm using a newtonsoft.json library here.
Your Response string has some mistakes man, its not a valid json
just small modification to be done as below:
{
"code": 0,
"message": "success",
"students": {
"details": {
"hjeke": {
"id": "257633000000070001",
"name": "hjeke",
"percentage": 36,
"type": "Good"
},
"Second": {
"id": "257633000000073001",
"name": "Second",
"percentage": 4,
"type": "bad"
}
}
}
}
you can make out the difference
Now Follow these steps:
1.Go to this link Json to C#
2.place your Json string there and generate C# class object
3.Now create this class in your solution
4.Now deserialize As below
var DeserialisedObject = JsonConvert.DeserializeObject<Your Class>(YourJsonString);
First, create the classes:
public class Hjeke
{
public string id { get; set; }
public string name { get; set; }
public int percentage { get; set; }
public string type { get; set; }
}
public class Second
{
public string id { get; set; }
public string name { get; set; }
public int percentage { get; set; }
public string type { get; set; }
}
public class Details
{
public List<Hjeke> hjeke { get; set; }
public List<Second> Second { get; set; }
}
public class Students
{
public List<Details> details { get; set; }
}
public class RootObject
{
public int code { get; set; }
public string message { get; set; }
public List<Students> students { get; set; }
}
After that, use JSON.NET to deserialize:
var deserialized = JsonConvert.DeserializeObject<Class1>(YourStringHere);
Do you have any influence over the json response? Details should probably be a JSONArray in this case, not an object with a varying amount of properties, since I assume that's what you mean is the issue here.
I'm trying to get objects from a multi-level JSON array. This is the an example table:
array(2) {
["asd"]=>
array(3) {
["id"]=>
int(777)
["profile"]=>
array(4) {
["username"]=>
string(5) "grega"
["email"]=>
string(18) "random#example.com"
["image"]=>
string(16) "http...image.jpg"
["age"]=>
int(26)
}
["name"]=>
string(5) "Grega"
}
["be"]=>
array(4) {
["username"]=>
string(5) "grega"
["email"]=>
string(18) "random#example.com"
["image"]=>
string(16) "http...image.jpg"
["age"]=>
int(26)
}
}
The string I'm trying to reach is either of the emails (example). This is how I try it:
public class getAsd
{
public string asd;
}
public class Profile
{
public string username { get; set; }
public string email { get; set; }
public string image { get; set; }
public string age { get; set; }
}
}
And then using JavaScriptSerilization.Deserilize<Asd>(jsonData); to deserilize it, but when I try the same with "Profile", it gives me the following error:
No parameterless constructor defined for type of 'System.String'.
JSON:
{"asd":{"id":777,"profile":{"username":"grega","email":"random#example.com","image":"http...image.jpg","age":26},"name":"Grega"},"be":{"username":"grega","email":"random#example.com","image":"http...image.jpg","age":26}}
And idea what might be wrong?
[EDIT: Smarm removed. OP did add JSON in an edit.]
Your profile class, as JSON, should resemble the following.
{
"username":"grega",
"email":"random#example.com",
"image":"http...image.jpg",
"age":"26",
"roles": [
{"name": "foo"},
{"name": "bar"}
]
}
array should not show up in JSON unless its part of a property name ("codearray") or property value("There's no 'array' in JSON. There's no 'array' in JSON.").
Arrays of objects in JSON are encased in square brackets [] and comma-delimited. An array/collection of profiles in JSON:
[
{
"username":"gretta",
"email":"mrshansel#example.com",
"image":"http...image.jpg",
"age":"37",
"roles": [
{"name": "foo"},
{"name": "bar"}
]
},
{
"username":"methusaleh",
"email":"old#men.org",
"image":"http...image.jpg",
"age":"2600",
"roles": [
{"name": "foo"},
{"name": "},
{"name": "bar"}
]
},
{
"username":"goldilocks",
"email":"porridge#bearshous.com",
"image":"http...image.jpg",
"age":"11",
"roles": [
{"name": "foo"}
]
}
]
While that may not fully answer your question, could you start with that and update your question?
EDIT:
See this answer by Hexxagonal for the complete approach.
Alright, here was what a "basic" version of your classes would be. You should really follow a standard of having properties have their first letter capitalized. Since you did not do this earlier, I maintained that style.
public class Type1
{
public TypeAsd asd { get; set; }
public TypeBe be { get; set; }
}
public class TypeAsd
{
public int id { get; set; }
public TypeBe profile { get; set; }
public string name { get; set; }
}
public class TypeBe
{
public string username { get; set; }
public string email { get; set; }
public string image { get; set; }
public int age { get; set; }
}
You deserialization code would then look something like the following:
string jsonString = "{\"asd\":{\"id\":777,\"profile\":{\"username\":\"grega\",\"email\":\"random#example.com\",\"image\":\"http...image.jpg\",\"age\":26},\"name\":\"Grega\"},\"be\":{\"username\":\"grega\",\"email\":\"random#example.com\",\"image\":\"http...image.jpg\",\"age\":26}}";
JavaScriptSerializer serializer = new JavaScriptSerializer();
Type1 obj = serializer.Deserialize<Type1>(jsonString);
json ="{
"data": [
{
"id": "1000",
"from": {
"name": "Anthony Waema",
"category": "message",
"id": "192"
},
"message": "this is the message",
"updated_time": "2001-05-06T19:34:15+0000",
"likes": {
"data": [
{
"id": "100001692250255",
"name": "\u00dcnal Turanl\u0131"
},
{
"id": "100001060078996",
"name": "S\u00e9f\u00e2 K\u00e2ql\u00e4Nn"
}]
},
{
"id": "10150186255753553",
"from": {
"name": "another name",
"category": "message",
"id": "100001"
},
"message": "this is the message",
"updated_time": "2001-04-06T19:34:15+0000",
"likes": {
"data": [
{
"id": "1002345",
"name": "\u00dcnal Turanl\u0131"
},
{
"id": "100234",
"name": "S\u00e9f\u00e2 K\u00e2ql\u00e4Nn"
}]
}
}
]
}";
public class Allstatus
{
public List<sdata> data { get; set; }
public scpaging paging { get; set; }
}
public class sdata
{
public string id { get; set; }
public sfrom from { get; set; }
public string message { get; set; }
public string updated_time {get; set;}
public List<likesdata> likes { get; set; }
}
public class likesdata
{
public string id{ get; set; }
public string name{ get; set; }
}
public class sfrom
{
public string name {get; set;}
public string category {get; set;}
public string id {get; set;}
}
JavaScriptSerializer ser = new JavaScriptSerializer();
page = ser.Deserialize<allstatus>(json);
foreach (sdata cd in page.data)
{
foreach(likesdata ld in cd.likes.data)
{
Console.WriteLine(ld.id+"\t"+ld.name);
}
}
problem:
I need to parse the json and retrieve likes data.
I can access "from" data but not "likes" data.. I get nullreference error when I do this. help needed here.. Thanks.
Edit2:
Referring https://gist.github.com/973510, its clear from the returned json, that if a particular facebook message doesnt have any likes, then the returned json doesnt contain a property called likes. Hence likes property of sdata object is null. Thats just how the server returns the data.
There are two ways you can deal with this. Either do a manual check whether likes is null. Or initialize the likes property in the sdata constructor. And initialize the likesdata list in the likesdatacollection constructor.
Code:
public class sdata
{
// other properties
public likedatacollection likes { get; set; }
public sdata()
{
likes = new likedatacollection();
}
}
public class likedatacollection
{
public List<likesdata> data { get; set; }
public likedatacollection()
{
data = new List<likesdata>();
}
}
This way, even if fb doesnt return any likes, the constructors will initialize the properties, so they will not be null. You can then check whether likes.data.Count > 0. If yes, then fb returned likes, else fb didnt return likes.
Edit1:
From the OP's comment, its clear that the json is properly formed. Meaning, the json is as retrieved from some server api. Therefore it is the sdata class that is the culprit. Please look at this gist for the full solution.
The short version. For the simplest case, your c# classes need to follow the exact same structure as your json. As per the json, data has a property called likes. the likes object has a property called data which is an array of objects with properties id and name.
So your c# class sdata should have a property called likes of type likesdatacollection. This class should have a property data of type List<likesdata>...
Off topic, people generally seem to prefer Json.Net ... so you may want to use that. The reason I use it is because I need it to work in a .Net 2.0 code base ...
You should try running your JSON through a validator like JSON Lint. That should help you find any JSON errors.