2016-06-27 10 views
0

일부 데이터를 삭제할 수있는 JSON 문자열이 있습니다. 쉬운 방법으로는 거기에JSON 응답에서 요소 제거

{ 
    "Response": { 
    "BillHeader": { 
     "BillId": "7134", 
     "DocumentId": "MN003_0522060", 
     "ConversionValue": "1.0000", 
     "BillType": "Vndr-Actual", 
     "AccountDescription": "0522060MMMDDYY", 
     "AccountLastChangeDate": "06/07/2016" 
    } 
    }, 
    "Error": null 
} 

: 나는 다음과 같습니다하도록 "ResponseType": "VirtualBill", 부품을 제거 할 수 원하는 JSON 응답 이상에서

{ 
    "ResponseType": "VirtualBill", 
    "Response": { 
    "BillHeader": { 
     "BillId": "7134", 
     "DocumentId": "MN003_0522060", 
     "ConversionValue": "1.0000", 
     "BillType": "Vndr-Actual", 
     "AccountDescription": "0522060MMMDDYY", 
     "AccountLastChangeDate": "06/07/2016" 
    } 
    }, 
    "Error": null 
} 

: 아래

은 JSON 응답입니다 C#에서이 작업을 수행할까요?

답변

-1

여기 해결책이 있습니다.

var temp={"ResponseType": "VirtualBill", "Response": { "BillHeader": { "BillId": "7134", "DocumentId": "MN003_0522060", "ConversionValue": "1.0000", "BillType": "Vndr-Actual", "AccountDescription": "0522060MMMDDYY", "AccountLastChangeDate": "06/07/2016" } }, "Error": null }; 

delete temp.ResponseType; 

JSON.stringify(temp); 

이 출력을 제공 할뿐만 :

"{"응답 "{"BillHeader "{"BillId ":"7134 ","DocumentId ":"MN003_0522060 ","ConversionValue를 ":" 1.0000 ","BillType ":"Vndr-Actual ","AccountDescription ":"0522060MMMDDYY ","AccountLastChangeDate ":"06/07/2016 "}},"오류 ": null}"

+0

안녕 Prateek, 내가 JSON = "ResponseType"가 "VirtualBill" "응답": { "BillHeader": { "BillId": "7134", "DocumentId": "MN003_0522060",,363,210 "ConversionValue를": "1.0000" "BillType", "Vndr-실제" "AccountDescription": "0522060MMMDDYY" "AccountLastChangeDate": "2016년 6월 7일" } }, "오류" : null } 코드를 제공해 주실 수 있습니까? – vsreekanth

+0

자바 스크립트처럼 보입니다. 나는 OP가 C# 코드를 찾고 있다고 생각한다. –

+0

오 예, 저는 자바 스크립트를 제공했습니다. C# 코드를 제공하겠습니다. –

0

JsonObject 키를 제거한 다음 string으로 다시 변환하십시오. Json.Net를 사용

3

,이 같은 원하지 않는 속성을 제거 할 수 있습니다

JObject jo = JObject.Parse(json); 
jo.Property("ResponseType").Remove(); 
json = jo.ToString(); 

바이올린 : https://dotnetfiddle.net/BgMQAE

-1
Sample sample= new Sample(); 
var  properties=sample.GetType().GetProperties().Where(x=>x.Name!="ResponseType"); 
var response = new Dictionary<string,object>() ; 

foreach(var prop in properties) 
{ 
    var propname = prop.Name; 
    response[propname] = prop.GetValue(sample); ; 
} 

var response= Newtonsoft.Json.JsonConvert.SerializeObject(response); 
관련 문제