2016-06-27 24 views
0

いくつかのデータを削除したいJSON文字列があります。以下はJSONレスポンスからの要素の削除

JSONレスポンスです:JSONレスポンス以上から

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

私ができるようにしたい、それはこのようになりますよう "ResponseType": "VirtualBill",一部削除:

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

をする簡単な方法はあります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" 、 "文書ID": "MN003_0522060"、 "ConversionValue":」 「AccountLastChangeDate」:「06/07/2016」}}、「Error」:null}「

+0

こんにちはPrateek、 私はJSON = "ResponseType" を持っている: "VirtualBill"、 "応答":{ "BillHeader":{ "BillId": "7134"、 "DocumentId": "MN003_0522060"、 "ConversionValue": "1.0000"、 "BillType"、 "Vndr、実際の" "AccountDescription": "0522060MMMDDYY"、 "AccountLastChangeDate": "2016年6月7日" }}、 "エラー" :null } コードを入力してください。 – vsreekanth

+0

これはjavascriptのようです。私はOPがC#コードを探していたと思う。 –

+0

ああ、私はJavaScriptを提供しています。私にC#コードを提供しましょう。 –

0

「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); 
関連する問題