简体   繁体   English

在C#中解析选定的JSON键值对

[英]Parsing selected JSON key value pair in C#

I have a JSON Input and i need to store specific key value from it. 我有一个JSON输入,我需要从中存储特定的键值。

For eg: Lets says, i have input like below 例如:让我们说,我输入如下

I/P: I / P:

{
 "CLICKS": "0.14",
 "IMPRESSIONS": 0,    
 "SOCIAL": 0,    
 "REACH": 0,    
 "ACTIONS": 0  
}

O/P:(In String Form) O / P :(字符串形式)

{ "CLICKS": "0.14"}

I am using JObject 我正在使用JObject

var finalJsonData = JObject.Parse(jsonInStringForm);

I can try with JToken or SelectToken, but that will make it more complex, thats why looking for more optimized solution or inbuilt feature in C# libs. 我可以尝试使用JToken或SelectToken,但这会使它变得更加复杂,这就是为什么在C#库中寻找更优化的解决方案或内置功能的原因。

Do you want something like this? 你想要这样的东西吗?

JObject input = JObject.Parse(@"{
 ""CLICKS"": ""0.14"",
 ""IMPRESSIONS"": 0,    
 ""SOCIAL"": 0,    
 ""REACH"": 0,    
 ""ACTIONS"": 0  
}");
JProperty find = input.Property("CLICKS");
JObject output = new JObject(find);
string s = output.ToString();

If you want to remove single property: 如果要删除单个属性:

JObject input = JObject.Parse(@"{
 ""CLICKS"": ""0.14"",
 ""IMPRESSIONS"": 0,    
 ""SOCIAL"": 0,    
 ""REACH"": 0,    
 ""ACTIONS"": 0  
}");
JProperty find = input.Property("IMPRESSIONS");
find.Remove();
string s = input.ToString().Dump();

Copy your JSON code { "CLICKS": "0.14", "IMPRESSIONS": 0,
"SOCIAL": 0,
"REACH": 0,
"ACTIONS": 0
}
复制您的JSON代码{ "CLICKS": "0.14", "IMPRESSIONS": 0,
"SOCIAL": 0,
"REACH": 0,
"ACTIONS": 0
}
{ "CLICKS": "0.14", "IMPRESSIONS": 0,
"SOCIAL": 0,
"REACH": 0,
"ACTIONS": 0
}
{ "CLICKS": "0.14", "IMPRESSIONS": 0,
"SOCIAL": 0,
"REACH": 0,
"ACTIONS": 0
}
Then inside Visual Studio create an class from the pasted JSON data.
{ "CLICKS": "0.14", "IMPRESSIONS": 0,
"SOCIAL": 0,
"REACH": 0,
"ACTIONS": 0
}
然后在Visual Studio中,根据粘贴的JSON数据创建一个类。
Edit -> Paste Special -> Paste JSON as Classes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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