gson解析嵌套的json对象 [英] gson parsing nested json objects

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

问题描述

我试图解析一个json字符串到java对象。目前的代码是手工读取文件和生成java对象。但是,我想把执行到gson。

I am trying to parse a json string to java object. Currently the code is manually reading file and generating java object. However, I am looking to take the implementation to gson.

这是我从Web服务调用收到的json:

Here is the json that I receive from the web service call:

{ "comment": [
        "This file is used to define the behavior for the elements parsed.",
        "Each entry in the file will have the format of element:name, skip:bool",
        "If SkipFlag is true, it means that element need not be processed.",
        "Convention used for elements and rule names is camelCase"
    ],
    "rules": [ { "element": "html", "skip": true },
               { "element": "head", "skip": true },
               { "element": "head", "skip": true },
               { "element": "body", "skip": true }
    ]
}

我需要忽略注释和转换规则。这里是我试图为规则java对象定义的java类型:

I need to ignore the comments and convert rules. Here is the java type I am trying to define for rules java object:

// Arraylist < Map < elementname, Map < name, value >  > >
ArrayList< Map<String, Map<String, String>  > > rules;

有一个简单的方法来做这个与gson?

Is there an easy way of doing this with gson?

推荐答案

您可以声明特定的类:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import com.google.gson.Gson;

class Rule {
   String  element;
   boolean skip;
}

class ElementParser {
   String[] comment;
   Rule[]   rules;
}

public class JSonDecoder {
   public static void main( String[] args ) throws IOException {
      try( BufferedReader reader =
              new BufferedReader( new FileReader( "Skip.json" ))) {
         System.out.println( 
            new Gson().fromJson( reader, ElementParser.class ).toString());
      }
   }
}

长版本:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import com.google.gson.Gson;

class Rule {
   String element;
   boolean skip;

   @Override public String toString() {
      StringBuilder sb = new StringBuilder();
      sb.append( '\t' );
      sb.append( element );
      sb.append( " ==> " );
      sb.append( skip );
      sb.append( '\n' );
      return sb.toString();
   }   
}
class ElementParser {
   String[] comment;
   Rule[]    rules;

   @Override public String toString() {
      StringBuilder sb = new StringBuilder();
      sb.append( "Comment:\n" );
      for( String c : comment ) {
         sb.append( '\t' );
         sb.append( c );
         sb.append( '\n' );
      }
      sb.append( "Rules:\n" );
      for( Rule r : rules ) {
         sb.append( r.toString());
      }
      return sb.toString();
   }
}

public class JSonDecoder {
   public static void main( String[] args ) throws IOException {
      try( BufferedReader reader = new BufferedReader( new FileReader( "Skip.json" ))) {
         System.out.println( 
            new Gson().fromJson( reader, ElementParser.class ).toString());
      }
   }
}

结果:

Comment:
    This file is used to define the behavior for the elements parsed.
    Each entry in the file will have the format of element:name, skip:bool
    If SkipFlag is true, it means that element need not be processed.
    Convention used for elements and rule names is camelCase
Rules:
    html ==> true
    head ==> true
    head ==> true
    body ==> true

这篇关于gson解析嵌套的json对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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