循环通过c#中的json响应

时间:2015-08-20 05:23:57

标签: c# json json.net json-deserialization

我有一个json响应,此时我能够进入单个节点。我无法遍历响应以获取特定值,即' PropertyValue'并将其存储在列表中。

以下是回复和代码:

{
"documents":[
    {
        "document":[
            {
                "propertyname":"Id",
                "propertyvalue":"{1ED8A008-959D-4D54-9B3D-48F10F5CED85}"
            },
            {
                "propertyname":"DVALinkedDocId",
                "propertyvalue":""
            },
            {
                "propertyname":"DVAPageNumber",
                "propertyvalue":""
            }
        ]
    },
    {
        "document":[
            {
                "propertyname":"Id",
                "propertyvalue":"{FCDACA2E-98BD-4E7F-BB4B-F1A543B40E3A}"
            },
            {
                "propertyname":"DVALinkedDocId",
                "propertyvalue":""
            },
            {
                "propertyname":"DVAPageNumber",
                "propertyvalue":""
            }
        ]
    },
    {
        "document":[
            {
                "propertyname":"Id",
                "propertyvalue":"{3DC1C70F-35AE-43AA-8CEC-FACE3ACEA519}"
            },
            {
                "propertyname":"DVALinkedDocId",
                "propertyvalue":""
            },
            {
                "propertyname":"DVAPageNumber",
                "propertyvalue":""
            }
        ]
    },
    {
        "document":[
            {
                "propertyname":"Id",
                "propertyvalue":"{B397323D-C0D4-4DA2-A95A-A79C3842D2DE}"
            },
            {
                "propertyname":"DVALinkedDocId",
                "propertyvalue":""
            },
            {
                "propertyname":"DVAPageNumber",
                "propertyvalue":""
            }
        ]
    }
]
}

这是我的代码:

DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(GuidResponse));
using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(responseData)))
{
     var response = (GuidResponse)serializer.ReadObject(ms);
     foreach (var obj in response.documents)
     {
          foreach (var docObj in obj.document)
          {
              string Guid = docObj.propertyvalue;
          }
     }
}

问题是我想在列表中显示所有guid属性值,但我无法使用上面的代码执行此操作。

3 个答案:

答案 0 :(得分:1)

创建一个字符串类型数组或列表,其中包含您通过Json传递给c#程序的所有对象。 在C#中创建一个模型类,并将数据序列化到该模型。

string[] AchievementsList = Regex.Matches(strAchievements, @"\{.*?\}").Cast<Match>().Select(m => m.Value).ToArray();
for (i = 0; i < AchievementsList.Length; i++)
{
    TestClass aJson = js.Deserialize<TestClass>(AchievementsList[i]); //use aJson object to access properties you  need 
}

用于保存json类型对象的类

class TestClass
{
    public  string Name { get; set; }
}

答案 1 :(得分:0)

这对我有用:

 var list = response.documents.SelectMany(d => d.document).Select(o => o.propertyvalue).Where(r => !string.IsNullOrEmpty(r)).ToList();

答案 2 :(得分:0)

或者您可以使用JSON.Net -

public class Document2
{
    public string propertyname { get; set; }
    public string propertyvalue { get; set; }
}

public class Document
{
    public List<Document2> document { get; set; }
}

public class RootObject
{
    public List<Document> documents { get; set; }
}

然后 -

var o = JsonConvert.DeserializeObject<RootObject>(new StreamReader("json").ReadToEnd());



var list = new List<string>();

foreach (var item in o.documents)
{
    list.Add(item.document[0].propertyvalue);
}