C# 在c中循环通过json数组#
我有一个json字符串,比如C# 在c中循环通过json数组#,c#,json,parsing,C#,Json,Parsing,我有一个json字符串,比如 {"objectType" : "Subscriber", "objectList":[{"firstName":"name1","email":"email@example.com","address":"exampleAddress"},{"firstName":"name2","email":"email2@example.com","address":"exampleAddress2"}]} 我需要在我的C代码中解析它。我试过了 JavaScriptSer
{"objectType" : "Subscriber", "objectList":[{"firstName":"name1","email":"email@example.com","address":"exampleAddress"},{"firstName":"name2","email":"email2@example.com","address":"exampleAddress2"}]}
我需要在我的C代码中解析它。我试过了
JavaScriptSerializer json_serializer = new JavaScriptSerializer();
object routes_list = json_serializer.DeserializeObject(myjson here);
但是我不能循环遍历“objectList”数组。如何做到这一点?var routes\u list=(字典)json\u serializer.DeserializeObject(myjson);
var jsonObj = new JavaScriptSerializer().Deserialize<RootObj>(json);
foreach (var obj in jsonObj.objectList)
{
Console.WriteLine(obj.address);
}
public class ObjectList
{
public string firstName { get; set; }
public string email { get; set; }
public string address { get; set; }
}
public class RootObj
{
public string objectType { get; set; }
public List<ObjectList> objectList { get; set; }
}
foreach(路由列表中的var记录)
{
控制台写入线(记录);
}
这对我来说很有效,基本上可以将JSON转换为YAML
string JSONDeserialized {get; set;}
public int indentLevel;
private bool JSONDictionarytoYAML(Dictionary<string, object> dict)
{
bool bSuccess = false;
indentLevel++;
foreach (string strKey in dict.Keys)
{
string strOutput = "".PadLeft(indentLevel * 3) + strKey + ":";
JSONDeserialized+="\r\n" + strOutput;
object o = dict[strKey];
if (o is Dictionary<string, object>)
{
JSONDictionarytoYAML((Dictionary<string, object>)o);
}
else if (o is ArrayList)
{
foreach (object oChild in ((ArrayList)o))
{
if (oChild is string)
{
strOutput = ((string)oChild);
JSONDeserialized += strOutput + ",";
}
else if (oChild is Dictionary<string, object>)
{
JSONDictionarytoYAML((Dictionary<string, object>)oChild);
JSONDeserialized += "\r\n";
}
}
}
else
{
strOutput = o.ToString();
JSONDeserialized += strOutput;
}
}
indentLevel--;
return bSuccess;
}
字符串JSONDeserialized{get;set;}
公共层面;
private bool JSONDictionarytoYAML(字典目录)
{
bool bsucces=假;
高级++;
foreach(dict.Keys中的字符串strKey)
{
字符串strOutput=“.padlefit(indentLevel*3)+strKey+”:“;
JSONDeserialized+=“\r\n”+strOutput;
对象o=dict[strKey];
if(o是字典)
{
JSONDictionarytoYAML((字典)o);
}
else if(o是ArrayList)
{
foreach(对象oChild in((ArrayList)o))
{
if(oChild是字符串)
{
strOutput=((字符串)oChild);
JSONDeserialized+=strOutput+“,”;
}
else if(oChild是字典)
{
JSONDictionarytoYAML((字典)oChild);
JSONDeserialized+=“\r\n”;
}
}
}
其他的
{
strOutput=o.ToString();
JSONDeserialized+=strOutput;
}
}
缩进级别--;
返回b成功;
}
用法
Dictionary<string, object> JSONDic = new Dictionary<string, object>();
JavaScriptSerializer js = new JavaScriptSerializer();
try {
JSONDic = js.Deserialize<Dictionary<string, object>>(inString);
JSONDeserialized = "";
indentLevel = 0;
DisplayDictionary(JSONDic);
return JSONDeserialized;
}
catch (Exception)
{
return "Could not parse input JSON string";
}
Dictionary JSONDic=newdictionary();
JavaScriptSerializer js=新的JavaScriptSerializer();
试一试{
JSONDic=js.反序列化(inString);
JSONDeserialized=“”;
等级=0;
显示字典(JSONDic);
返回JSONDeserialized;
}
捕获(例外)
{
return“无法解析输入JSON字符串”;
}
可能的副本能否显示您的TargetType
请快速回复。工作正常。没有类ObjectList和RootObj是否可以工作?我是否需要为此添加任何dll?@wingsofire是的,答案中有链接。我最喜欢的json解析器。Newtonsoft.json呢?
string JSONDeserialized {get; set;}
public int indentLevel;
private bool JSONDictionarytoYAML(Dictionary<string, object> dict)
{
bool bSuccess = false;
indentLevel++;
foreach (string strKey in dict.Keys)
{
string strOutput = "".PadLeft(indentLevel * 3) + strKey + ":";
JSONDeserialized+="\r\n" + strOutput;
object o = dict[strKey];
if (o is Dictionary<string, object>)
{
JSONDictionarytoYAML((Dictionary<string, object>)o);
}
else if (o is ArrayList)
{
foreach (object oChild in ((ArrayList)o))
{
if (oChild is string)
{
strOutput = ((string)oChild);
JSONDeserialized += strOutput + ",";
}
else if (oChild is Dictionary<string, object>)
{
JSONDictionarytoYAML((Dictionary<string, object>)oChild);
JSONDeserialized += "\r\n";
}
}
}
else
{
strOutput = o.ToString();
JSONDeserialized += strOutput;
}
}
indentLevel--;
return bSuccess;
}
Dictionary<string, object> JSONDic = new Dictionary<string, object>();
JavaScriptSerializer js = new JavaScriptSerializer();
try {
JSONDic = js.Deserialize<Dictionary<string, object>>(inString);
JSONDeserialized = "";
indentLevel = 0;
DisplayDictionary(JSONDic);
return JSONDeserialized;
}
catch (Exception)
{
return "Could not parse input JSON string";
}