tom tom - 3 months ago 10
JSON Question

JSON.net search for JSON and create new file in C#

I have got this JSON output:

[
{
"Type": "05A",
"Price": "6.8.",
"valueaa": "okay",
"valueab": "aaa",
"valueac": "aaa",
"valuead": "aaa",
"valueae": "aaa",
"valueaf": "aaa"
},
{
"Type": "05A",
"Price": "6.8.",
"valueaa": "okay",
"valueab": "aaa",
"valueac": "aaa",
"valuead": "aaa",
"valueae": "aaa",
"valueaf": "aaa"
},
{
"Type": "05A",
"Price": "7.8.",
"valueaa": "error",
"valueab": "aaa",
"valueac": "aaa",
"valuead": "aaa",
"valueae": "aaa",
"valueaf": "aaa"
},
{
"Type": "05A",
"Price": "7.8.",
"valueaa": "aaa",
"valueab": "aaa",
"valueac": "aaa",
"valuead": "aaa",
"valueae": "aaa",
"valuef": "aaa"
}]


Now I need to filter the whole file and create new ones which contain all types for one price.
For example my output would be:

[
{
"Type": "05A",
"Price": "6.8.",
"valueaa": "okay",
"valueab": "aaa",
"valueac": "aaa",
"valuead": "aaa",
"valueae": "aaa",
"valueaf": "aaa"
},
{
"Type": "05A",
"Price": "6.8.",
"valueaa": "okay",
"valueab": "aaa",
"valueac": "aaa",
"valuead": "aaa",
"valueae": "aaa",
"valueaf": "aaa"
}]


and in another file:

[{
"Type": "05A",
"Price": "7.8.",
"valueaa": "error",
"valueab": "aaa",
"valueac": "aaa",
"valuead": "aaa",
"valueae": "aaa",
"valueaf": "aaa"
},
{
"Type": "05A",
"Price": "7.8.",
"valueaa": "error",
"valueab": "aaa",
"valueac": "aaa",
"valuead": "aaa",
"valueae": "aaa",
"valueaf": "aaa"
}]


At the moment I use json.net (Newtonsoft) in C#, maybe there is a solution with this lib.

class Temp_Felder
{
public string Price{ get; set; }
public string Type{ get; set; }
public string valueaa{ get; set; }
public string valueab{ get; set; }
public string valueac{ get; set; }
public string valuead{ get; set; }
public string valueae{ get; set; }
public string valueaf{ get; set; }
}


Here I have the input:

public void detailplan()
{ using (StreamReader file = File.OpenText(@"C:\test\temp.json"))
{
JsonSerializer serializer = new JsonSerializer();
Temp_Felder temp_felder2 = (Temp_Felder)serializer.Deserialize(file, typeof(Temp_Felder));
}
}


But how do I have to continue now?

BWA BWA
Answer

Working solution:

using System.Collections.Generic;
using System.IO;
using System.Linq;
using Newtonsoft.Json;

namespace StackOwerflow
{
    class Temp_Felder
    {
        public string Price { get; set; }
        public string Type { get; set; }        
        public string valueaa { get; set; }        
        public string valueab { get; set; }       
        public string valueac { get; set; }       
        public string valuead { get; set; }       
        public string valueae { get; set; }        
        public string valueaf { get; set; }
    }
    public class Program
    {
        static public void Main()
        {
            string json = File.ReadAllText(@"C:\karol\json.txt");

            List<Temp_Felder> j = JsonConvert.DeserializeObject<List<Temp_Felder>>(json);

            var grpByPrice = j.GroupBy(r => r.Price);


            foreach (var i in grpByPrice)
            {
                string grp = JsonConvert.SerializeObject(i);

                string fileName = string.Format(@"C:\karol\json_{0}.txt", i.Key);

                File.WriteAllText(fileName, grp);            
            }            
        }
    }
}