Reputation: 423
I have a Json file with couple of fields and I have some problems searching for specific fields.
This is the Json file(I cut it short since the original is huge):
{
"errors": {
"errorCode": 0,
"errorMessage": "",
"errorDescription": null
},
"pagination": {
"recordsReturned": 250,
"totalRecordsFound": 123,
"currentPage": 1,
"recordsPerPage": 250
},
"data": {
"totalDCount": 1713,
"totalValue": "50",
"totalCarats": 60,
"averagePricePerCarat": 21,
"averageDiscount": -0.29,
"dResult": [
{
"color": "H",
"dID": 4693,
"fancyColor": {
"dominantColor": null,
"secondaryColor": null,
"overtones": null,
"intensity": null,
"color1": null,
"color2": null
},
"seller": {
"accountID": 124,
"companyName": "",
"companyCode": " ",
"founded": "",
"address": null,
"telephone": " ",
"fax": null,
"email": null,
"contactPrimaryName": "value",
"city": null,
"state": null,
"country": "USA",
"address1": null,
"address2": null,
"skypeName": null,
"primarySupplierBadge": true,
"ratingPercent": 1.0,
"totalRating": 1.0,
"relatedAccounts": null
},
"shape": "Round",
{
"color": "H",
"dID": 46,
"fancyColor": {
"dominantColor": null,
"secondaryColor": null,
"overtones": null,
"intensity": null,
"color1": null,
"color2": null
},
"seller": {
"accountID": 124,
"companyName": "",
"companyCode": " ",
"founded": "",
"address": null,
"telephone": " ",
"fax": null,
"email": null,
"contactPrimaryName": "value",
"city": null,
"state": null,
"country": "USA",
"address1": null,
"address2": null,
"skypeName": null,
"primarySupplierBadge": true,
"ratingPercent": 1.0,
"totalRating": 1.0,
"relatedAccounts": null
},
"shape": "Round"
}
]
}
}
I wrote a code that should search for "dId" field value under the "dResult". Unfortunately this error comes up (I am using Newtonsoft.Json parser) :
"Newtonsoft.Json.JsonReaderException: Invalid property identifier character: {. Path 'data.dResult[0].shape', line 54, position 11."
A.This is the code I wrote, I would be happy if you could tell me what is the problem ?
B.A second problem that I had is that I need to pick only the "dID" of those that have the "shape" field value as "Round", I didn't figure out a way to do that since I need to go and find a further field while encountering "dId" field .
class Program
{
static void Main(string[] args)
{
string filepath = "../../json1.json";
string result = string.Empty;
string str = string.Empty;
using (StreamReader r = new StreamReader(filepath))
{
var json = r.ReadToEnd();
JObject jObject = JObject.Parse(json);
JToken jUser = jObject["data"];
string jsonString = jUser.ToString();
JObject jObject1 = JObject.Parse(jsonString);
JToken jUser2 = jObject1["dResult"];
string jsonString2 = jUser2.ToString();
JObject jObject2 = JObject.Parse(jsonString2);
foreach (var item in jObject2.Properties())
{
if (item.Name == "dID")
{
str = item.Value.ToString();
result = result + " " + str;
}
}
}
Console.WriteLine(result);
}
}
Reference for the comment I received here (Another Json section, this is under "dResult"):
, {
"dID": 281242,
"seller": {
"accountID": 21321,
"companyName": "RA",
"companyCode": "001",
"founded": "000",
"address": null,
"telephone": "999",
"fax": null,
"email": null,
"contactPrimaryName": "name",
"city": null,
"state": null,
"country": "USA",
"address1": null,
"address2": null,
"skypeName": null,
"primarySupplierBadge": true,
"ratingPercent": 1.0,
"totalRating": 1.0,
"relatedAccounts": null
},
"shape": "Round",
"size": 0.010,
"color": "K",
"fancyColor": {
"dominantColor": null,
"secondaryColor": null,
"overtones": null,
"intensity": null,
"color1": null,
"color2": null
},
Upvotes: 1
Views: 9929
Reputation: 3293
You can use the following Linq query to pull the dID values for the Round shapes. However the JSON is not in a correct format.
var jsonStr = File.ReadAllText(Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
"example-json.json"));
var parsed = JObject.Parse(jsonStr);
var dIdList = parsed["data"]["dResult"]
.Where(x => x.Value<String>("shape").Equals("round", StringComparison.InvariantCultureIgnoreCase))
.Select(x => x.Value<Int32>("dID"))
.ToList();
And here is the re-formatted JSON (notice the diff on line 52-53), there was a missing }
which caused the array to be off:
Once you correct the JSON, by adding this }
and run the above Linq query it will return the following result:
Good, luck. Let me know if you need any further assistance.
Upvotes: 2
Reputation: 13533
Once you fix the improperly formatted json data, you can use linq to find the round objects:
var searchResults = from r in jObject["data"]["dResult"]
where r["shape"].ToString() == "Round"
select r;
foreach (var r in searchResults)
{
Console.WriteLine(r["dID"]);
}
Upvotes: 0