2016-12-23 4 views
0

jsonでNull値を取得しています。私はjsonに変換するためにJavaScriptSerializerを使用しています。私の問題を解決する方法を教えてください。前もって感謝します。csonを使用してjsonでNULLを削除するにはどうすればよいですか?

JSON:

"Boardingpoints": [{ 
    "location": "Shapur,", 
    "id": 2776, 
    "time": "08:45PM" 
}, { 
    "location": "Ameerpet,Jeans Corner", 
    "id": 6, 
    "time": "09:00PM" 
}, { 
    "location": "Vivekananda Nagar Colony,", 
    "id": 2347, 
    "time": "08:45PM" 
}, { 
    "location": "Nampally,", 
    "id": 2321, 
    "time": "09:30PM" 
}, { 
    "location": "Kondapur,Toyota Show room", 
    "id": 2244, 
    "time": "09:10PM" 
}, { 
    "location": "KUKATPALLY,JNTU", 
    "id": 2230, 
    "time": "08:30PM" 
}, null, null, { 
    "location": "L.B Nagar,Near Petrol Bunk", 
    "id": 2247, 
    "time": "09:00PM" 
}, null, null, { 
    "location": "Moosapet,", 
    "id": 2781, 
    "time": "10:30PM" 
}, null, null, null, null, null, null, { 
    "location": "S.R.Nagar bus stop,S.R.Nagar bus stop", 
    "id": 4090, 
    "time": "10:00PM" 
}], 

クラス:

public class Bordingpoints 
     { 
      public string location { get; set; } 
      public int id { get; set; } 
      public string time { get; set; } 

     } 

デシリアライズ:私は、オブジェクトの一部をスキップし、いくつかの条件に基づいて

var bp = new Bordingpoints[array1.Count]; 
        for (int i = 0; i < array1.Count; i++) 
        { 
         try 
         { 
          JArray pickups = (JArray)array1[i]["boardingPoints"]; 
          for (int j = 0; j < pickups.Count; j++) 
          { 
           string location = (string)pickups[j]["location"]; 
           int id = (int)pickups[j]["id"]; 
           string time = (string)pickups[j]["time"]; 

           if (!bpid.Contains(id)) 
           { 
            bp[i] = new Bordingpoints(); 
            bp[i].location = location; 
            bp[i].id = id; 
            bp[i].time = time; 
            bpid.Add(id); 

           } 
          } 
         } 


JavaScriptSerializer js = new JavaScriptSerializer(); 

        string bdpt = js.Serialize(bp); 

、その時のために、私はnull値を取得しています。私を助けてください。

+0

string bdpt = js.Serialize(bp); 

を交換してそこより良い方法は、おそらくですが、これはトリックを行う必要があります。 – npinti

+0

[JavaScriptSerializerはヌル/デフォルト値でプロパティを除外できますか?](http://stackoverflow.com/questions/1387755/can-javascriptserializer-exclude-properties-with-null-default-values) –

+0

@npintiを参照してくださいコード –

答えて

3

あなたの配列には空のアイテムがあります。あなたは次のことを試すことができます。

は、あなたが実際の直列化復元を行うために使用しているコードを追加する必要があります

string bdpt = js.Serialize(bp.Where(p=>p!=null)); 
+0

ありがとうございます。ワーキング –

関連する問題