How to Parse Json list in VB.NET Newtonsoft - json

I receive from a webservice, the following JSON:
[ {"lat": 42.41375, "user_id": 762, "user": "John", "lng": 23.02187}, {"lat": 42.46835, "user_id": 675, "user": "Mike", "lng": 23.02612}, {"lat": 42.85672, "user_id": 654, "user": "Jane", "lng": 23.01029}, {"lat": 42.46876, "user_id": 687, "user": "Luke", "lng": 23.02676} ]
I want to add this information using VB.net, row by row, to a DataGridView.
I'm new to JSON.net.
How to loop through the whole list?
How to go about parsing it?

As per as my knowledge there are multiple ways to do it, i prefer following way which is more easy ,straight and maintainable
there is JSON serializer and deserializer provided inbuilt in .net framework, requirement for that is, you have to create classes which will map to your JSON. you can have a look at or http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.deserialize.aspx
In the above case you have to create your class like below
class UserLatLang
{
public long lat { get;set;}
public long lng { get;set;}
public long user_id {get;set;}
public string user {get;set;}
}
after this you can
var serializer = new JavaScriptSerializer();
var listofUserLatLang = serializer.Deserialize<UserLatLang>(responseText);
and you will get a list of UserLatLang in listofUserLatLang
or you can also refer class from http://msdn.microsoft.com/en-us/library/bb412179.aspx
Once you get list of UserLatLang you can directly bind it to DataGrid
Hope this solves your problem
thanks,
Sandesh Daddi

Related

How to convert reddit posts to list in dart?

I want to get posts from Reddit API. Posts are in "children" node but each object has another object inside.
Can somebody help me write a function that convert this JSON to a list of dart objects?
Here is JSON string.
{
"kind": "Listing",
"data": {
"after": "t3_zzzhq4",
"dist": 2,
"children": [
{
"kind": "t3",
"data": {
"selftext": "blablabla",
"author_fullname": "3xblabla",
"title": "moreblabla",
"created": 1672515982,
"id": "10020p0"
}
},
{
"kind": "t3",
"data": {
"selftext": "blablabla",
"author_fullname": "3xblabla",
"title": "moreblabla",
"created": 1672515982,
"id": "10020p0"
}
}
],
"before": null
}
}
I tried all the tutorials on the topic of complex json parsing, but none of them met my needs. I would know how to parse simple json, but here it is deeply nested JSON, which bothers me a lot, and i cant quite grasp it. Appreciete any help.
Solution:
First go to json to dart and paste JSON string, this generator will make you all classes needed for your JSON.
Then you will need to decode string:
final jsonResponse = json.decode(jsonString);
And then deserialize your JSON like this:
List postslist = list.map((i) => Post.fromJson(i['data'])).toList();
For me, it was crucial i['data']. After adding that, i could deserialize all objects that were living inside that node. Thanks everyone! Hope that someone else this will be helpful! Cheers.

Unable to Parse Array from Returned JSON using Moshi

I've started using Moshi along with Retrofit 2 and have run into an issue parsing an array of objects within the parent object that is returned from the service call. The returned JSON looks like this:
{
"acf": {
"email": "dirk#dirkgently.com",
"address": "24 Cortland Avenue",
"country": "US",
"description": "Oh my goodness",
"created_at": "1416672067",
"updated_at": "1416672067",
"facebook": "",
"contact": "Dirk Gently",
"photos": [
{
"file": 3525
},
{
"file": 3526
},
{
"file": 6110
},
{
"file": 3527
},
{
"file": 3528
},
{
"file": 6700
},
{
"file": 7404
},
{
"file": 7419
}
],
"latitude": "40.801249",
"longitude": "-99.746280"
}
}
I'm getting the following exception with the 'photos' field:
com.squareup.moshi.JsonDataException: Expected BEGIN_ARRAY but was
STRING at path $[0].acf.photos
I've defined the models like so:
public class Acf {
private String email;
private String address;
List<Photo>photos;
}
public class Photo {
public int file;
}
I've tried declaring the property in the model in various other ways, and read through the docs to try to figure out what I'm doing wrong. I'm stuck at this point and could really use a second set of eyes to point me in the right direction. I have a feeling I'm missing something painfully simple and obvious. Thanks in advance to anyone that can help. If I've left out any pertinent info let me know and I'll update the post.
Update: I've found that this exception only occurs when I fetch multiple ACFs. If I do a request for a single object everything parses correctly.
Based on the error it sounds like the endpoint you're calling is possibly returning different types for that parameter (i.e. a string when you're expecting an array). Try changing it from a List<> to an Object and see if the call succeeds. You should be able to type-check the Object and cast whatever you need from it. An alternative would be to use a custom type adapter, but I'm not sure how to accomplish that using Moshi. Good luck.

Microsoft.Graph Unable to deserialize the response

I am quite new to programming and especially Microsoft.Graph
I am having problems handling the response to:
https://graph.microsoft.com/v1.0/me/drive/root/children
the response looks like this (just much longer):
{
"#odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('xyz%40hotmail.com')/drive/root/children",
"value": [
{
"createdBy": {
"user": {
"displayName": "xyz",
"id": "cf58e4781082"
}
},
"createdDateTime": "2009-01-08T08:52:07.063Z",
"cTag": "adDpFREJDR4RTQMTgxMDgyITEyOC42MzYxODM0MTU0Mjc3MDAwMDA",
"eTag": "aRURCQ0Y1OEU0A4MiExMjguMA",
"id": "EDBCF58E471082!128",
"lastModifiedBy": {
"user": {
"displayName": "xyz",
"id": "edbcf58e48082"
}
}, ............. etc...
The response that I received is correct, in JSON format (I believe ><), but I cannot figure out how to parse it into an array containing the folders name.
Please help!
Have considered using the Microsoft Graph client library? It will deserialize the JSON. Your call will look like this:
// Return all children files and folders off the drive root.
var driveItems = await graphClient.Me.Drive
.Root
.Children
.Request()
.GetAsync();
foreach (var item in driveItems)
{
// Get your item information
}
Here's some samples to help you get started:
https://github.com/microsoftgraph?utf8=%E2%9C%93&q=csharp
You can use the JavaScriptSerializer to do this. Assuming
//json contains the JSON Response
var jsonOutput = new System.Web.Script.Serialization.JavaScriptSerializer();
jsonOutput.DeserializeObject(json);
This has been discussed earlier. See this thread: Easiest way to parse JSON response
Refer this link for JavaScriptSerializer: https://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer(v=vs.110).aspx

How can I get values from decoded file

I have a JSON file. When I encoded and decoded it, it gives me a resultset like below:
{
"_id": 519188,
"name": "Novinki",
"country": "RU",
"coord": {
"lon": 37.666668,
"lat": 55.683334
}
}
How can I get the values from that?
If you are working with javascript, you need to store the contents of json file into a javascript variable. From there you can easily access the values.
eg:- var someVariable = decodedFileContent;
var id = someVariable._id;
Hope it helps.Give more information.

C++ XE2 - How to read Nested JSON?

I have been searching quite a while now for how to read a nested JSON with C++ Builder XE2 TJSONObject.
There are a few examples in Delphi but they use the TJSONValue object, but in the C++ version this class has a pure virtual function and can not be created.
Some example JSON:
{
"totalHits": 4170,
"totalCount": 4170,
"startIndex": 0,
"adverts": [
{
"Id": "14380005",
"companyInfo": {
"companyName": "Clarion Hotel Sign",
"orgNumber": "5564660107",
"companyText": "hotell"
},
"address": {
"streetName": "Street race 2",
"postCode": "101 26",
"postArea": "MY AREA",
"postBox": "Box 310"
},
"homepage": "www.mypage.net"
}
]
}
The whole JSON is stored in the JSON object, trust me, it's in there :)
TJSONObject *JSON = new TJSONObject;
I have no problem to get the value for totalHits and totalCount, but how do I get the "companyName" value?!?
Thanks
I post this belated answer for someone else who meets the same problems in the future....
TJSONArray* jArray = (TJSONArray* )JSON->Get("adverts")->JsonValue;
TJSONObject* jCompanyInfo = (TJSONObject*)((TJSONObject*)jArray->Get(0))
->Get("companyInfo")->JsonValue);
String companyName = jCompanyInfo->Get("companyName")->JsonValue->Value());