将此json字符串解析为字符串数组c#

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13721686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:34:31  来源:igfitidea点击:

Parse this json string to string array c#

c#arraysjsonparsing

提问by mdc

Feels like there is a one-two row solution for what I want to do: Parse a string like this:

感觉就像我想做的事情有一个一两行的解决方案:解析这样的字符串:

 "{\"postalcode\":\"12345\",\"postalcity\":\"SOME-CITY\",\"country\":\"UK\",\"box\":false}"

Into something like this:

变成这样:

    string[] result = { "12345", "SOME-CITY", "UK", "false" };

Whats the simplest way to do this?

什么是最简单的方法来做到这一点?

采纳答案by L.B

string json = "{\"postalcode\":\"12345\",\"postalcity\":\"SOME-CITY\",\"country\":\"UK\",\"box\":false}";

var dict = new JavaScriptSerializer().Deserialize<Dictionary<string,object>>(json);
var postalCode = dict["postalcode"];

//Array is also possible
string[] result = dict.Select(kv => kv.Value.ToString()).ToArray();

回答by James

You could use JavaScriptSerializerto serialize the json into a dynamicobject which would allow you to access the properties via name e.g.

您可以使用JavaScriptSerializer将 json 序列化为一个dynamic对象,该对象将允许您通过名称访问属性,例如

var address = new JavaScriptSerializer().Deserialize<dynamic>(json);
Console.WriteLine(address["postalcode"]);

回答by VRC

You could also use newtonsoft : http://james.newtonking.com/pages/json-net.aspx

您也可以使用 newtonsoft :http://james.newtonking.com/pages/json-net.aspx

string json = @"{
  ""Name"": ""Apple"",
  ""Expiry"": new Date(1230422400000),
  ""Price"": 3.99,
  ""Sizes"": [
    ""Small"",
    ""Medium"",
    ""Large""
  ]
}";

JObject o = JObject.Parse(json);

string name = (string)o["Name"];
// Apple

JArray sizes = (JArray)o["Sizes"];

string smallest = (string)sizes[0];
// Small

I found another related post : JSON to string array in C#
Lib : http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx

我发现了另一个相关的帖子:JSON to string array in C#
Lib:http: //msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx

回答by Sina Iravanian

It looks like your input string is a JSON string, for which you can use a JSON deserializer if you want. If not you can use regular-expression along with named-groups as the following:

看起来您的输入字符串是一个 JSON 字符串,如果需要,您可以使用 JSON 反序列化器。如果没有,您可以将正则表达式与命名组一起使用,如下所示:

List<string> values = new List<string>();
List<string> keys= new List<string>();

string pattern = @"\""(?<key>[^\""]+)\""\:\""?(?<value>[^\"",}]+)\""?\,?";
foreach(Match m in Regex.Matches(input, pattern))
{
    if (m.Success)
    {
        values.Add(m.Groups["value"].Value);
        keys.Add(m.Groups["key"].Value);
    }
}

var result = values.ToArray();

Named groups in regular-expression are indicated by (?<group-name>pattern). In the above pattern we have two named groups: key, and valuewhich can be grabbed from the Matchobject using the Groupsindexer.

正则表达式中的命名组由 表示(?<group-name>pattern)。在上面的模式中,我们有两个命名组:key, 和value可以Match使用Groups索引器从对象中抓取。