c# - Linq to json filtering results with Where clause -


i'm new json. i'm trying filter json data using linq json query in c#. i'm trying retrieve multiple values json data:

string json= @"{  "parts": [     {        "attributes": {         "motherboard": "gigabyte ga-h81m-s2h",         "max ram": "16gb",         "form factor": "micro atx",         "ram slots": 2,         "socket / cpu": "lga1150"       },       "type": "motherboard",       "name": "gigabyte ga-h81m-s2h",       "slug": "gigabyte-motherboard-gah81ms2h"     },     {         "attributes": {         "motherboard": "msi h55-g43",         "max ram": "16gb",         "form factor": "atx",         "ram slots": 4,         "socket / cpu": "lga1156"       },       "type": "motherboard",       "name": "msi h55-g43",       "slug": "msi-motherboard-h55g43"     },     {       "url": "http://pcpartpicker.com/part/asus-motherboard-rampageivblackedition",       "attributes": {         "motherboard": "asus rampage iv black edition",         "max ram": "128gb",         "form factor": "eatx",         "ram slots": 8,         "socket / cpu": "lga2011"       },       "type": "motherboard",       "name": "asus rampage iv black edition",       "slug": "asus-motherboard-rampageivblackedition"     }  ],  }"; 

this c# code:

jobject results = jobject.parse(json);   var categories = c in results["parts"]["attributes"].children()["motherboard"].values<string>()                          group c c                          g                          orderby g.count() descending                          select new { category = g.key, count = g.count() }; 

i tried code , not returns result.please let me know i'm doing mistake or proper way write query.can please me solve this.

you can use selecttokens queries of nature. supports jsonpath query syntax. thus:

        var categories = c in results.selecttokens("parts[*].attributes.motherboard")                          group c c                              g                              orderby g.count() descending                              select new { category = (string)g.key, count = g.count() }; 

in json, value of "parts" : [...] array; in query, "parts[*]" wildcard searches through elements of array.


Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -