在python

时间:2016-06-28 17:01:53

标签: python json

请有人解释如何将以下json数据转换为python中的字符串。它很大但我需要你的帮助...... 您可以通过以下链接查看: - http://api.openweathermap.org/data/2.5/forecast/daily?q=delhi&mode=json&units=metric&cnt=7&appid=146f5f89c18a703450d3bd6737d4fc94 请建议它的解决方案对我的项目很重要: - )

3 个答案:

答案 0 :(得分:0)

您可以在python中解码JSON字符串,如下所示:

public class Person implements Runnable
{
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void run() {
        // to fill up later
    }
}

public class Main {
        private static int nameCount = 1;
        public static List<Person> list;

        public static void main(String[] args) {
            list = new ArrayList<Person>();
            for (int i = 0; i < 5; i++) {
                list.add(new Person(getPersonName(), getPersonAge());
            }
            list.removeIf(p -> p.getage().equals(18));
            for (Person person : list) {
                System.out.println(persion);
            } 
        }

        public static String getPersonName() {
            String nameto = "PERSON" + String.valueOf(nameCount);
            nameCount++;
            return nameto;
        }

        public static int getPersonAge() {
            Random ran = new Random();
            int ageperson = ran.nextInt(25) + 1;
            return ageperson;
        }
        //how to make an instance of Thread after filtering the person
    }

来源:https://docs.python.org/2/library/json.html

答案 1 :(得分:0)

import requests

url = 'http://api.openweathermap.org/data/2.5/forecast/daily?q=delhi&mode=json&units=metric&cnt=7&appid=146f5f89c18a703450d3bd6737d4fc94'

response = requests.get(url)

response.text # this is a string
response.json() # this is a json dictionary

s = "The City is {city[name]} todays HIGH is {list[0][temp][max]}".format(**response.json())
print s

答案 2 :(得分:0)

一些简单的代码将从您的页面读取JSON并生成Python字典。我使用了相邻字符串的隐式连接来改进代码的布局。

import json
import urllib.request
f = urllib.request.urlopen
      (url="http://api.openweathermap.org/data/2.5/forecast/daily?"
           "q=delhi&mode=json&units=metric&"
           "cnt=7&appid=146f5f89c18a703450d3bd6737d4fc94")
content = f.read()
result = json.loads(content.decode("utf-8"))
print(result)

这给了我以下输出(我没有在代码样式中显示,因为它会出现在一个长行中):

{&#39; city&#39;:{&#39; coord&#39;:{&#39; lat&#39;:28.666668,&#39; lon&#39;:77.216667},&#39 ; country&#39;:&#39; IN&#39;,&#39; id&#39;:1273294,&#39; population&#39;:0,&#39; name&#39;:&#39;德里&#39;},&#39; cnt&#39;:7,&#39;消息&#39;:0.0081,&#39; list&#39;:[{&#39; dt&#39;:1467093600, &#39;天气&#39;:[{&#39; icon&#39;:&#39; 01n&#39;,&#39; id&#39;:800,&#39; description&#39;:& #39;晴朗的天空&#39;主要&#39;:&#39;清除&#39;},&#39;湿度&#39;:82,&#39;云&#39;:0 ,&#39;压力&#39;:987.37,&#39;速度&#39;:2.63,&#39; temp&#39;:{&#39; max&#39;:32,&#39; eve&# 39 ;:32,&#39;晚上&#39;:30.67,&#39; min&#39;:30.67,&#39; day&#39;:32,&#39; morn&#39;:32}, &#39; deg&#39;:104},{&#39; dt&#39;:1467180000,&#39;天气&#39;:[{&#39; icon&#39;:&#39; 10d&# 39;,&#39; id&#39;:501,&#39;描述&#39;:&#39;中雨&#39;,&#39;主要&#39;:&#39; Rain&#39; },&#39;湿度&#39;:74,&#39;云&#39;:12,&#39;压力&#39;:989.2,&#39;速度&#39;:4.17,&#39雨&#39;:9.91 ,&#39; temp&#39;:{&#39; max&#39;:36.62,&#39; eve&#39;:36.03,&#39; night&#39;:31.08,&#39; min&# 39;:29.39,&#39; day&#39;:35.61,&#39; morn&#39;:29.39},&#39; deg&#39;:126},{&#39; dt&#39;: 1467266400,&#39;天气&#39;:[{&#39;图标&#39;:&#39; 02d&#39;,&#39; id&#39;:801,&#39;描述&#39; :&#39;很少的云&#39;主要&#39;:&#39;云&#39;}],&#39;湿度&#39;:71,&#39;云&#39; :12,&#39;压力&#39;:986.56,&#39;速度&#39;:3.91,&#39; temp&#39;:{&#39; max&#39;:36.27,&#39;前夕&#39;:35.19,&#39;晚上&#39;:30.87,&#39; min&#39;:29.04,&#39; day&#39;:35.46,&#39; morn&#39;:29.04 },&#39; deg&#39;:109},{&#39; dt&#39;:1467352800,&#39;天气&#39;:[{&#39; icon&#39;:&#39; 10d&#39;,&#39; id&#39;:502,&#39;描述&#39;:&#39;重度强降雨&#39;主要&#39;:&#39; Rain& #39;}],&#39;湿度&#39;:100,&#39;云&#39;:48,&#39;压力&#39;:#984.48,&#39;速度&#39;:0, &#39;下雨&#39;:18.47,&#39; temp&#39;:{&#39; max&#39;:30.87,&#39; eve&#39;:30.87,&#39; nig ht&#39;:28.24,&#39; min&#39;:24.96,&#39; day&#39;:27.16,&#39; morn&#39;:24.96},&#39; deg&#39;: 0},{&#39; dt&#39;:1467439200,&#39;天气&#39;:[{&#39;图标&#39;:&#39; 10d&#39;,&#39; id&# 39;:501,&#39;描述&#39;:&#39;中雨&#39;主要&#39;:&#39; Rain&#39;},&#39;湿度&# 39;:0,&#39;云&#39;:17,压力&#39;:983.1,&#39;速度&#39;:6.54,&#39;下雨&#39;:5.31,& #39; temp&#39;:{&#39; max&#39;:35.48,&#39; eve&#39;:32.96,&#39; night&#39;:27.82,&#39; min&#39; :27.82,&#39; day&#39;:35.48,&#39; morn&#39;:29.83},&#39; deg&#39;:121},{&#39; dt&#39;:1467525600, &#39;天气&#39;:[{&#39; icon&#39;:&#39; 10d&#39;,&#39; id&#39;:501,&#39; description&#39;:& #39;中雨&#39;主要&#39;:&#39; Rain&#39;},&#39;湿度&#39;:0,&#39;云&#39;:19 ,&#39;压力&#39;:984.27,&#39;速度&#39;:3.17,&#39;下雨&#39;:7.54,&#39; temp&#39;:{&#39; max&# 39;:34.11,&#39; eve&#39;:34.11,&#39; night&#39;:27.88,&#39; min&#39;:27.53,&#39; day&#39;:33.77, &#39; morn&#39;:27.53},&#39; deg&#39;:133},{&#39; dt&#39;:1467612000,&#39;天气&#39;:[{&#39; ;图标&#39;:&#39; 10d&#39;&#39; id&#39;:503,&#39;描述&#39;:&#39;非常大雨&#39;,&#39;主要&#39;:&#39; Rain&#39;},&#39;湿度&#39;:0,&#39;云&#39;:60,&#39;压力&#39;:984.82,& #39;速度&#39;:5.28,&#39;下雨&#39;:54.7,&#39; temp&#39;:{&#39; max&#39;:33.12,&#39; eve&#39; :33.12,&#39;晚上&#39;:26.15,&#39; min&#39;:25.78,&#39; day&#39;:31.91,&#39; morn&#39;:25.78},&# 39; deg&#39;:88}],&#39; cod&#39;:&#39; 200&#39;}