Click here to Skip to main content
15,881,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This my JSON
{
  "samples": [
    {
      "date": "2000-08-10T09:00:00Z",
      "tomato": 10,
      "papaya": 4,
      "pineapple": 4,
      "coconut": 4,
      "apple": 10
    },
    {
      "date": "2014-08-12T09:05:00Z",
      "tomato": 10.5,
      "papaya": 5,
      "coconut": 4,
      "pineapple": 4
    },
    {
      "date": "2000-08-14T09:02:00Z",
      "tomato": 10.8,
      "papaya": 3,
      "pineapple": 4
    },
    {
      "date": "2000-08-16T09:02:00Z",
      "tomato": 11.2,
      "papaya": 6,
      "coconut": 4
    },
    {
      "date": "2000-08-18T08:58:00Z",
      "tomato": 10.9,
      "papaya": 10,
      "coconut": 4,
      "pineapple": 4,
      "apple": 10
    },
    {
      "date": "2000-08-20T09:10:00Z",
      "tomato": 9.3,
      "papaya": 6,
      "pineapple": 4
    },
    {
      "date": "2000-08-22T09:01:00Z",
      "tomato": 9.5,
      "papaya": 10,
      "coconut": 4,
      "pineapple": 4,
      "apple": 10
    }
  ]
}


im trying to display the results like this

             parameter   LOW AVG MAX
             tomato        x   y   z
             pineaple      x   y   z
             papaya        x   y   z
             apple         x   y   z
             coconut       x   y   z

This is my results, i know its wrong but i need help on how to display the like indicated above.
             papaya=4   apple=10 pineaple=4  tomato=10    coconut=4
             papaya=5,  apple=0  pineaple=4  tomato=10.5  coconut=4
             papaya=3,  apple=0  pineaple=4  tomato=10.8  coconut=0
             papaya=6,  apple=0  pineaple=0  tomato=11.2  coconut=4
             papaya=10, apple=10 pineaple=4  tomato=10.9  coconut=4
             papaya=6,  apple=0  pineaple=4  tomato=9.3   coconut=0
             papaya=10, apple=10 pineaple=4  tomato=9.5   coconut=4


What I have tried:

private void PrintOverview(byte[] data)
{
string jsonStr = Encoding.UTF8.GetString(data);


var jObject = JObject.Parse(jsonStr);

var samples = JsonConvert.DeserializeObject<sample[]>(jObject["samples"].ToString());

var allParams = samples.Select(s => new { s.pH, s.nitrate, s.phosphate, s.temperature, s.chloride });


foreach (var parameter in allParams)
{

var allValues = samples.Where(x => x.tomato== parameter.tomato|| x.papaya
== parameter.papaya || x.pineapple== parameter.pineapple||
x.apple== parameter.apple|| x.coconut ==
parameter.coconut)
.Select(s => new { s.papaya, s.apple, s.pineapple, s.tomato,
s.coconut})
.ToList();


Console.WriteLine("{0} {1} {2}", parameter, allValues.Min(),allValues.Max());
}

}
public class Sample
{
public DateTime date { get; set; }
public double totmato{ get; set; }
public int papaya { get; set; }
public int pineapple { get; set; }
public int coconut { get; set; }
public int apple { get; set; }
}

public class RootObject
{
public List<sample> samples { get; set; }

}
Posted
Updated 26-Mar-19 8:42am
v2

1 solution

First thing first, your example json structure is incorrect.
How to test
-> Copy your json text
Chrome:
-> Hit F12
-> paste Your json text into console
Firefox:
-> Hit F12
-> x=(paste your json text)

Both will throw error.
You have created JSON object as
{
  {
   "sample": [
     {...},
     {...}
   ] 
  }
}

{} is considered as object, and each object must have to have name
{
  "Some_Name": {
    "sample":[
      {...},
      {...}
    ]
  }
}

is valid.


And also, I consider this JSON structure as poorly designed.
A better one would be
[
  {
    "date": "Some Date",
    "fruits": [
      {
        "name":"apple", 
        "price": 10
      }, 
      {
        "name": "orange", 
        "price": 20
      }
    ]
  }
]
 
Share this answer
 

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