在c#中循环一个json响应



我有一个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属性值,但我不能用上面的代码做到这一点。

创建一个字符串类型的数组或列表,其中包含通过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; }
}

这对我有用:

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

您也可以使用JSON。净——

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);
}

相关内容

  • 没有找到相关文章

最新更新