2016-12-23 1 views
0

json에서 Null 값을 얻고 있습니다. json으로 변환하기 위해 JavaScriptSerializer를 사용하고 있습니다. 아무도 내 문제를 해결하는 방법을 알려주십시오. 미리 감사드립니다.C#을 사용하여 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); 

. 도와주세요.

+0

string bdpt = js.Serialize(bp); 

교체가 더 나은 방법은 아마,하지만이 트릭을 할해야합니다. – npinti

+0

[JavaScriptSerializer가 null/기본값으로 속성을 제외 할 수 있습니까?] (http://stackoverflow.com/questions/1387755/can-javascriptserializer-exclude-properties-with-null-default-values) –

+0

@npinti를 참조하십시오. 코드 –

답변

3

배열에 Null 항목이 있습니다. 당신은 다음을 시도 할 수 있습니다.

당신은 당신이 실제 직렬화를 만들기 위해 사용하는 코드를 포함해야합니다

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

정말 고마워요. 일 –

관련 문제