簡體   English   中英

如何將此JSON結果解析為對象?

[英]How do I parse this JSON Result as an object?

我需要將此JSON字符串解析為我的“ WeatherJson”類型的對象。 但是,我不知道如何解析字符串中的數組,例如'“ weather”: [{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}] 實體類會是什么樣?

JSON字符串:

{
  "coord": {"lon":79.85,"lat":6.93},
  "sys": {
    "type": 1, 
    "id": 7864, 
    "message": 0.0145,
    "country": "LK",
    "sunrise": 1435883361,
    "sunset": 1435928421
  },
  "weather": [
     {"id":802, "main":"Clouds", "description":"scattered clouds", "icon":"03d"}
  ],
  "base": "stations",
  "main": {
    "temp": 302.15,
    "pressure": 1013,
    "humidity": 79,
    "temp_min": 302.15,
    "temp_max": 302.15
  },
  "visibility":10000,
  "wind": { "speed": 4.1, "deg": 220 },
  "clouds": { "all": 40 },
  "dt": 1435893000,
  "id":1248991,
  "name":"Colombo",
  "cod":200
}

編輯

我需要從代碼中檢索以下值:

WeatherJson w = new WeatherJson();
Console.WriteLine(w.weather.description);
//that above line was retrieved and stored from the JSONArray named 'weather' in the main json response

您只需要使JSON中的數組與POCO中的列表或數組類型匹配即可。 這是使用您提供的JSON的簡短但完整的示例:

using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;

class Test
{ 
    static void Main(string[] args) 
    {
        var json = File.ReadAllText("weather.json");
        var root = JsonConvert.DeserializeObject<Root>(json);
        Console.WriteLine(root.Weather[0].Description);
    }
}

public class Root
{
    // Just a few of the properties
    public Coord Coord { get; set; }
    public List<Weather> Weather { get; set; }
    public int Visibility { get; set; }
    public string Name { get; set; }
}

public class Weather
{
    public int Id { get; set; }
    public string Description { get; set; }
}

public class Coord
{
    public double Lon { get; set; }
    public double Lat { get; set; }
}

嘗試這樣:

void Main()
{
    var json = System.IO.File.ReadAllText(@"d:\test.json");

    var objects = JArray.Parse(json); // parse as array  
    foreach(JObject root in objects)
    {
        foreach(KeyValuePair<String, JToken> app in root)
        {
            var appName = app.Key;
            var description = (String)app.Value["Description"];
            var value = (String)app.Value["Value"];

            Console.WriteLine(appName);
            Console.WriteLine(description);
            Console.WriteLine(value);
            Console.WriteLine("\n");
        }
    }
}

嘗試此希望對您有幫助。

      var dataObj=JSON.parse {"coord":{"lon":79.85,"lat":6.93},"sys":
      {"type":1,"id":7864,"message":0.0145,"country":"LK",
 "sunrise":1435883361,
"sun        set":1435928421},"weather":     [{"id":802,"main":"Clouds",
    "description":"scattered    clouds","icon":"03d"}],
  "base":"stations","main":{"temp":302.15,"pressure":1013,
"humidity":79,"temp_min":302.15,
 "temp_max":302.15},"visibility":10000,"wind":{"speed":4.1,"deg":220},
 "clouds":{"all":40},"dt":1435893000,"id":1248991,
  "name":"Colombo","cod":200}

to read this .
i m reading one single string so u can able to know.

var windData=dataObj.wind.speed;
 it will read value 4.1 from your string like this..

If u want to read array of string then..
var weatherObj=dataObj.weather[0].description;

so it will read  description value from array.like this u can read.

嘗試兩次迭代,

for (key in parent) {
     for(key1 in parent[key]){
     //
     }
} 

JObject為此定義了方法Parse:

JObject json = JObject.Parse(JsonString);

您可能要參考Json.NET 文檔

如果您有對象集合,則可以使用JArray

JArray jarr = JArray.Parse(JsonString);

有關文檔,請參見此處

將JArry轉換為列表只需將以下方法放入。 它將返回您所需要的。

Jarray.ToObject<List<SelectableEnumItem>>()

您可以使用JavaScriptSerializer類將JSON解析為Object。

引用此StackOverflow線程

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM