Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
3.33/5 (2 votes)
See more:
I've to verify response coming from a web service as Json with c# Nunit asserts like

C#
Assert.AreEqual(Response.get(PARTICULAR FIELD), "expected value");


The o/p is like
Java
{
"meta": {
    "code": 200,
    "status": "ok",
    "expires_in": 60578
  },
}

Please suggest me a reusable method
Posted
Updated 31-Oct-14 5:24am
v2
Comments
Herman<T>.Instance 31-Oct-14 11:24am    
what have you tried?

1 solution

After you import Json.NET[^] package you can do the following:
C#
dynamic json = JsonConvert.DeserializeObject(yourJsonAsString)

Then you can preform your asserts
 
Share this answer
 
Comments
Member 11195373 4-Nov-14 1:52am    
I'm able to retrieve "apiVersion" value with above code in the below json with (json.apiVersion) but unable to access other values that are in arrays.
Can you please help me how to retrieve the value of container id and parentId in below json

{
"apiVersion": "0.0.0.224",
"meta": {
"metaInformation": {
"count": 50,
"offset": 0,
"filters": "",
"elapsedTimeInMS": 166
},
"createdAt": "2014-11-03T10:36:21.0654796Z"
},
"recordCount": {
"totalRecordsFound": 6,
"startingRecord": 0,
"recordsReturned": 6
},
"payload": [
{
"id": "8d301081-d47a-4221-ae21-c4d5406d112e",
"container": {
"id": "6189e1fa-296f-45f4-8b96-a7a32fe64bd3",
"type": "OrgUnit"
},
"parentId": "e6834c2e-446b-4aa9-8cb3-fa832c4565d3",
"description": "Long description goes here",
"tree": {
"name": "name1"
},
"subcategories": {
"total": 0,
"startIndex": 0,
"count": 0,
"items": null
},
"record": {
"createdAt": "2014-10-30T09:50:47.8251969Z",
"createdBy": {
"id": "00000000-0000-0000-0000-000000000000",
"username": "Not Implemented Yet."
},
"modifiedAt": "2014-10-30T09:50:47.8251969Z",
"modifiedBy": {
"id": "00000000-0000-0000-0000-000000000000",
"username": "Not Implemented Yet."
}
},
"_functions": {
"self": "categoryDetails",
"functions": [
"categories",
"categoryDetails",
"categorySubcategories",
"categoryAssets"
]
}
},
{
"id": "e254f292-d232-4a98-9f54-f60befbe4cdd",
"container": {
"id": "6189e1fa-296f-45f4-8b96-a7a32fe64bd3",
"type": "OrgUnit"
},
"parentId": "e6834c2e-446b-4aa9-8cb3-fa832c4565d3",
"description": "mediavalet",
"tree": {
"name": "name2"
},
"subcategories": {
"total": 0,
"startIndex": 0,
"count": 0,
"items": null
},
"record": {
"createdAt": "2014-10-17T12:08:31.9096807Z",
"createdBy": {
"id": "20eed301-4386-4ea3-9582-6ab39b08b59f",
"username": "Not Implemented Yet."
},
"modifiedAt": "2014-10-17T12:08:31.9096807Z",
"modifiedBy": {
"id": "20eed301-4386-4ea3-9582-6ab39b08b59f",
"username": "Not Implemented Yet."
}
},
"_functions": {
"self": "categoryDetails",
"functions": [
"categories",
"categoryDetails",
"categorySubcategories",
"categoryAssets"
]
}
}
]
}
Deflinek 4-Nov-14 9:01am    
Reply


Arrays are of type JArray ( http://james.newtonking.com/json/help/index.html?topic=html/T_Newtonsoft_Json_Linq_JArray.htm )
You can access it like:
json.payload[0]._functions.functions[3].Value
Member 11195373 5-Nov-14 8:31am    
Thanks for the info

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900