首页 \ 问答 \ 如何阅读这个JSON(How to read this JSON)

如何阅读这个JSON(How to read this JSON)

String json =     
[ 
 {"a":{"aa":"string","ab":"string"} },
 {"b":{"ba":"string","bb":"string"} }
]

我试图使用JsonObject解析这些数据。 当我使用此代码时:

JSONStringer Js= new JSONStringer(json);
 Log.d("json", ""+Js);

它给了我这个:( json的第一行,但我想要所有数据)

{"a":{"aa":"string","ab":"string"} } 

我怎么能用Android阅读这篇文章?


String json =     
[ 
 {"a":{"aa":"string","ab":"string"} },
 {"b":{"ba":"string","bb":"string"} }
]

I am trying to parse this data using JsonObject. When i use this code :

JSONStringer Js= new JSONStringer(json);
 Log.d("json", ""+Js);

it gives me this : (the first line of json but i want all data)

{"a":{"aa":"string","ab":"string"} } 

How can I read this with Android ?


原文:
更新时间:2022-04-04 06:04

最满意答案

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ReadFile {
  public static void main(String[]args) throws IOException{

      FileReader in = new FileReader("C:/test.txt");
      BufferedReader br = new BufferedReader(in);
      String line = "";
      String wholeReportText="";
      while ((line = br.readLine()) != null) {
         wholeReportText += line;
      }
      in.close();

           Pattern pattern = Pattern.compile("Movies(.*?)");
           Matcher matcher = pattern.matcher(wholeReportText);
           // check all occurance
           while (matcher.find()) {
                   System.out.println(matcher.group(1));
           }

      Pattern daypat = Pattern.compile("Tuesday (.*?)");
      Matcher daymat = daypat.matcher(wholeReportText);
      // check all occurance
      while (daymat.find()) {
            System.out.println(matcher.group(1));
      }


  }
}

在算法的情况下,您的问题需要在子问题中打破

步骤1)列出需要解析的所有文件(java NIO中的java FileFilter中的文件API和java7中的Paths API)

步骤2)逐个读取文件并从中提取数据(上面的示例代码)

步骤3)将提取的数据存储在一些HashMap或ArrayList中

步骤4)根据您的要求使用集合


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ReadFile {
  public static void main(String[]args) throws IOException{

      FileReader in = new FileReader("C:/test.txt");
      BufferedReader br = new BufferedReader(in);
      String line = "";
      String wholeReportText="";
      while ((line = br.readLine()) != null) {
         wholeReportText += line;
      }
      in.close();

           Pattern pattern = Pattern.compile("Movies(.*?)");
           Matcher matcher = pattern.matcher(wholeReportText);
           // check all occurance
           while (matcher.find()) {
                   System.out.println(matcher.group(1));
           }

      Pattern daypat = Pattern.compile("Tuesday (.*?)");
      Matcher daymat = daypat.matcher(wholeReportText);
      // check all occurance
      while (daymat.find()) {
            System.out.println(matcher.group(1));
      }


  }
}

Your Problem needs to be broke in sub problems as in case of algorithms

Step 1) To list all the files which you need to parse (File API in java FileFilter in java NIO and Paths API in java7)

Step 2) Read and extract data from file one by one (sample code above)

Step 3) Store the extracted data in some HashMap or ArrayList

Step 4) Use collections as per your requirement

相关问答

更多
  • 编写一些shell脚本 ,用必要的参数调用Java程序。 它可以像一两行一样简单。 如果将Java程序打包为包含所有必需依赖项的JAR,则可能会发现这更容易。 Write some sort of shell script which calls your Java program with the necessary arguments. It could be as simple as one or two lines. You may find this easier if you package y ...
  • 而不是在构造函数中使用exit() ing而是抛出IllegalArgumentException (因为那是真正发生的事情)并将其留给调用者来处理异常。 可以编写应用程序代码来处理异常,而JUnit测试可以断言发生异常。 Instead of exit()-ing in the constructor, throw an IllegalArgumentException instead (since that's what's really happening) and leave it to the c ...
  • 有春天的例子, PetClinic和JPetStore 。 在这些链接中,您可以找到更多应用程序。 There are spring examples, PetClinic and JPetStore. In these links you can find some more applications.
  • 我会自己回答。 我只是在报告中使用Dejavu Sans并将maven依赖项放在基本的jasper字体上,就是这样。 net.sf.jasperreports jasperreports-fonts 6.0.0
  • 通常的组合是使用TestNG或JUnit和Maven Surefire插件(如果你使用的是Maven)。 TestNG或JUnit将处理测试本身并为您生成报告,Maven Surefire将方便地将此报告放置到target/目录,以便您可以收集测试结果。 The usual combination is to use either TestNG or JUnit with Maven Surefire plugin (in case you are using Maven). TestNG or JUnit ...
  • 好的,您要做的第一件事就是将您的应用程序打包到一个jar文件中,并在清单中包含一个main-class。 Java教程中有一节介绍 。 然后,它取决于您要支持的环境。 最简单的方法就是为用户提供该jar文件。 在大多数桌面环境中,只需单击(或双击)文件即可从文件资源管理器运行jar文件。 您只需使用命令文件包装jar文件即可创建命令行版本。 在UNIX / Linux环境中,它就像它一样简单 #!/bin/bash java -jar myapp.jar 除此之外,您开始需要研究应用程序安装程序,这些程序 ...
  • 我会使用通用的功能测试自动化框架,并使用一组库来读取/解析/比较文件。 我熟悉Robot Framework,并且有一些Python库来读取/比较文件(一些嵌入在Robot本身,一些嵌入其他地方)。 这对于QA测试非常方便且易于使用。 查看演示项目以获得良好的开端。 如果您更喜欢坚持Java生态系统,可能需要尝试使用Cucumber-jvm或JBehave 。 I would use a generic functional test automation framework and use a set o ...
  • Arquillian被认为是集成测试的一个很好的选择。 然而,它看起来面向更“现代”的应用; 大多数示例都使用Java EE 5+和Maven。 我们正在使用J2EE和Ant。 我们目前也与Java 1.4绑定,虽然我们可能会迁移到Java 5,但我们不会很快升级到EJB 3.x. 我们也很可能坚持使用Ant。 考虑到这些限制因素,是否可以使用Arquillian? 注意:我是Arquillian的贡献者。 我试图在答案中不偏不倚。 这实际上取决于测试的执行方式。 如果您尝试使用Arquillian对容器内 ...
  • 我会用像Visual VM这样的东西来测试它。 它将在您的应用程序运行时实时显示内存,线程,CPU,创建的对象等内容。 我的好版本仅适用于Oracle / Sun JVM。 有一个随JDK一起提供的,但我不认为它显示的安装所有插件的版本都是1.6.3。 I'd instrument it with something like Visual VM. It'll show what's happening in memory, threads, CPU, objects created, etc. in re ...
  • import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ReadFile { public static void main(String[]args) throws IOException{ FileReader in ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)