简体   繁体   中英

Remove curly brackets from JObject

I receive a text which I parse as a JObject

txt = {"welcome":["Hello friend."],"image":"Y","IDs":{}}
var object = JObject.Parse(txt);

Among the values inside there is one called IDs which sometimes is empty. I get the value by

var mainID = object["IDs"].ToString();

And then I insert the value in a table. The problem is that when the value is empty it return with {}. So the insert statement fails. How can I remove the curly brackets when the value is empty?

Update based on comment

You could use the following

var mainID = (jobj["IDs"] is JValue)?(string)jobj["IDs"]:defaultValue;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM