json 中去除空格的方法

原理就是重新生成一段json,使用 newtonsoft, 写的不好,请多指教

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

namespace Jsonparse
{
    class Program
    {
        static void Main(string[] args)
        {
            string json = @"[[
    [{
            'CPU': 'Intel',
            'Drives': [
                'DVD read/writer',
                '500 gigabyte hard drive'
            ],
            'Mouses': {
                'one': '小米',
                'two': '戴尔'
            },
            'type': 'computer'
        },
        {
            'CPU': 'Test',
            'Drives': [
                'DVD read/writer',
                '500 gigabyte hard drive'
            ],
            'Mouses': {
                'one': '小米',
                'two': '戴尔',
                'type': 'computer'
            }
        }
    ], {
        'CPU': 'wss',
        'Drives': [
            'DVD read/writer',
            '500 gigabyte hard drive'
        ],
        'Mouses': {
            'one': '小米',
            'two': '戴尔',
            'type': 'computer'
        }
    }
]]";

            string needCopy = @"{
            'CPU ': 'Intel',
            'Drives ': [
                'DVD read/writer ',
                '500 gigabyte hard drive'
            ],
            'Mouses ': {
                'one ': '小米',
                'two ': '戴尔'
            },
            'type ': 'computer',
            'sameAs ':[],
             'Address':{
                'one ': 'address1',
                'two ': 'address ',
                 }
        }";

            JObject desJObject = new JObject();
            CopyJObject(JObject.Parse(needCopy), ref desJObject);
            string desStr = JsonConvert.SerializeObject(desJObject);

            JArray jArray = JArray.Parse(json);
            IList<JObject> list = new List<JObject>();
            GetJObject(jArray, ref list);

            foreach (var item in list)
            {
                Console.WriteLine(JsonConvert.SerializeObject(item));
            };
            Console.ReadKey();
            
        }

        public static void GetJObject(JArray jArray, ref IList<JObject> list)
        {
            foreach (var item in jArray)
            {
                if (item is JObject)
                    list.Add((JObject)item);
                if (item is JArray)
                    GetJObject((JArray)item, ref list);
            }
        }

        public static void CopyJObject(JObject orgJObject, ref JObject desJObject)
        {
            foreach (var pro in orgJObject)
            {
                if (pro.Value == null)
                    continue;
                if (pro.Value is JObject)
                {
                    JObject des = new JObject();
                    desJObject[pro.Key.Trim()] = des;
                    CopyJObject((JObject)pro.Value, ref des);
                }
                if (pro.Value is JValue)
                {
                    if (string.IsNullOrEmpty((string)pro.Value)) continue;
                    desJObject.Add(pro.Key.Trim(), pro.Value.ToString().Trim());
                }
                if (pro.Value is JArray)
                {
                    JArray array = new JArray();
                    foreach (var item in (JArray)pro.Value)
                    {
                        if (item is JObject)
                        {
                            JObject des = new JObject();
                            CopyJObject((JObject)item, ref des);
                            array.Add(des);
                        }
                        if (item is JValue)
                        {
                            array.Add(item.ToString());
                        }
                        
                    }
                    if (array.Count != 0) 
                    desJObject.Add(pro.Key.Trim(), array);
                }
            }
        }
    }
}

 

posted @ 2018-04-07 22:58  skywss27  阅读(6373)  评论(0编辑  收藏  举报