打印JSON值 [英] Printing JSON values

查看:131
本文介绍了打印JSON值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 forecast:{
txt_forecast:{
date:8:00 AM MST,
forecastday:[
{
period:0,
icon:partiallycloudy,
icon_url:http://icons-ak.wxug.com/i/c/k/partlycloudy.gif,
title:星期四,
fcttext:部分多云,最高63°,风速低于5英里/小时,
fcttext_metric:部分多云,高达17C,风速低于5公里/小时。 ,
pop:0
}

打印嵌套的JSON值。如果我想打印fcttext,我该如何去做呢? JSONParser parser = new JSONParser()();} $ J $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $' ;
尝试{
Object obj = parser.parse(new FileReader(C:\\ABC.json));

JSONObject jsonObject =(JSONObject)obj;
String a =(String)jsonObject.get(forecast)。toString();
System.out.println(a);
}
catch(FileNotFoundException e){
e.printStackTrace();

catch(IOException e){
e.printStackTrace();

catch(ParseException e){
e.printStackTrace();




$ b

我在做什么错误? 完整的JSON代码

首先,你的json是不正确的,使用jsonlint [1]来检查你的json是否格式正确。

>

将您的JSON格式化为以下内容:

  {
forecast :{
txt_forecast:{
date:8:00AMMST,
forecastday:[
{
period:0,
icon:partiallycloudy,
icon_url:http://icons-ak.wxug.com/i/c/k/partlycloudy.gif,
title :
fcttext:Partlycloudy.Highof63F.Windslessthan5mph。,
fcttext_metric:Partlycloudy.Highof17C.Windslessthan5km / h。,
pop: 0
}
]





$ p $使用下面的代码解析

  package com.aamir.stackoverflow; 

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
$ b $ public class JSONParserStackOverflow {
public static void main(String [] args){

String request ={\+
\forecast\:{\ n+
\txt_forecast \:{\ n+
\date \:\8 :AMMST \,\\\
+
\forecastday \:[\\\
+
{\ n+
\ \:0,\\\
+
\icon \:\partiallycloudy \,\\\
+
\icon_url\: \http://icons-ak.wxug.com/i/c/k/partlycloudy.gif\\",\\\
+
\title \:\星期四\,\\\
+
\fcttext \:\Partlycloudy.Highof6 3F.Windslessthan5mph.\,\\\
+
\fcttext_metric \:\Partlycloudy.Highof17C.Windslessthan5km / h.\,\\\
+
\pop \:\0 \\\\
+
} \\\
+
] \ n+
} \\ \\ n+
} \\\
+
};

JsonElement weatherJSON = new JsonParser()。parse(request);
JsonObject weatherObj = weatherJSON.getAsJsonObject();
JsonObject forecastObj = weatherObj.get(forecast)。getAsJsonObject();
JsonObject txt_forecast = forecastObj.get(txt_forecast)。getAsJsonObject();

JsonArray forecastDays = txt_forecast.getAsJsonArray(forecastday); (JsonElement forecastDay:forecastDays){
System.out.println(forecastDay.getAsJsonObject()。get(fcttext)。toString());





$ b $ / code $ / pre
$ b我已经使用Google的GSON [ 2]库来解析JSON。



[1] http:// jsonlint .com /



[2] https://code.google.com/p/google-gson/


Here's just a chunk of JSON code that I need to access...

"forecast":{
        "txt_forecast": {
        "date":"8:00 AM MST",
        "forecastday": [
        {
        "period":0,
        "icon":"partlycloudy",
        "icon_url":"http://icons-ak.wxug.com/i/c/k/partlycloudy.gif",
        "title":"Thursday",
        "fcttext":"Partly cloudy. High of 63F. Winds less than 5 mph.",
        "fcttext_metric":"Partly cloudy. High of 17C. Winds less than 5 km/h.",
        "pop":"0"
        }

Im having trouble printing nested JSON values. If I wanted to print the "fcttext", how would I go upon doing that? I've tried this...

public static void display() {
         JSONParser parser = new JSONParser();
         try {
             Object obj = parser.parse(new FileReader("C:\\ABC.json"));

         JSONObject jsonObject = (JSONObject) obj;
             String a = (String) jsonObject.get("forecast").toString();
             System.out.println(a);
     } 
         catch (FileNotFoundException e) {
         e.printStackTrace();
     } 
         catch (IOException e) {
         e.printStackTrace();
     } 
         catch (ParseException e) {
             e.printStackTrace();
     }
     }

What am I doing wrong? Full JSON code.

解决方案

First of all your json is not proper, use jsonlint[1] to check if your json is properly formatted.

Reformatting your JSON to following,

{
    "forecast": {
        "txt_forecast": {
            "date": "8: 00AMMST",
            "forecastday": [
                {
                    "period": 0,
                    "icon": "partlycloudy",
                    "icon_url": "http: //icons-ak.wxug.com/i/c/k/partlycloudy.gif",
                    "title": "Thursday",
                    "fcttext": "Partlycloudy.Highof63F.Windslessthan5mph.",
                    "fcttext_metric": "Partlycloudy.Highof17C.Windslessthan5km/h.",
                    "pop": "0"
                }
            ]
        }
    }
}

Use following code to parse,

package com.aamir.stackoverflow;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class JSONParserStackOverflow {
    public static void main(String[] args) {

        String request = "{\n" +
                "    \"forecast\": {\n" +
                "        \"txt_forecast\": {\n" +
                "            \"date\": \"8: 00AMMST\",\n" +
                "            \"forecastday\": [\n" +
                "                {\n" +
                "                    \"period\": 0,\n" +
                "                    \"icon\": \"partlycloudy\",\n" +
                "                    \"icon_url\": \"http: //icons-ak.wxug.com/i/c/k/partlycloudy.gif\",\n" +
                "                    \"title\": \"Thursday\",\n" +
                "                    \"fcttext\": \"Partlycloudy.Highof63F.Windslessthan5mph.\",\n" +
                "                    \"fcttext_metric\": \"Partlycloudy.Highof17C.Windslessthan5km/h.\",\n" +
                "                    \"pop\": \"0\"\n" +
                "                }\n" +
                "            ]\n" +
                "        }\n" +
                "    }\n" +
                "}";

        JsonElement weatherJSON = new JsonParser().parse(request);
        JsonObject weatherObj = weatherJSON.getAsJsonObject();
        JsonObject forecastObj = weatherObj.get("forecast").getAsJsonObject();
        JsonObject txt_forecast = forecastObj.get("txt_forecast").getAsJsonObject();

        JsonArray forecastDays = txt_forecast.getAsJsonArray("forecastday");


        for(JsonElement forecastDay : forecastDays) {
            System.out.println(forecastDay.getAsJsonObject().get("fcttext").toString());
        }
    }
}

I have used Google's GSON[2] library to parse JSON.

[1] http://jsonlint.com/

[2] https://code.google.com/p/google-gson/

这篇关于打印JSON值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆