`
冰糖葫芦有点酸
  • 浏览: 29503 次
  • 性别: Icon_minigender_2
  • 来自: 武汉
社区版块
存档分类
最新评论

Convert JSON File to JSON Object

    博客分类:
  • Java
 
阅读更多
Here is the JSON file as below
[
    {
      "id": 123,
      "properties": {
           "property1": property1,
           "property2": property1
       }
    },
    {
      "id": 456,
      "properties": {
           "property1": property1,
           "property2": property1
       }
    }
]

Requirements, get properties as JSON object which id is "123".

Code as below:

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

import java.io.FileReader;

/**
* Get test data from json file
*/
private JsonObject initTestDataFromJson (String fileName) {
    JsonParser parser = new JsonParser();
    JsonObject properties = null;

    try {
        String filePath = System.getProperty("user.dir").replace("\\", "/") + "/src/test/" + fileName + ".json";
        Object obj = parser.parse(new FileReader(filePath));

        JsonArray jsonArrays = (JsonArray) obj;
        for(JsonElement jsonElement : jsonArrays) {
            JsonObject jsonObject = jsonElement.getAsJsonObject();
            if ("123".equals(jsonObject.get("id").getAsString())) {
                properties = jsonObject.getAsJsonObject("properties");

                logger.info(properties.toString());

                break;
            }
        }
    } catch (Exception e) {
        logger.error(e);
    }
   return properties;
}

分享到:
评论

相关推荐

    python json文件转txt文件,批处理json文件转换成一个txt文件,

    在Python编程中,JSON(JavaScript Object Notation)和TXT是两种常见的数据格式。JSON格式用于存储和传输数据,而TXT则是一种简单的文本文件格式。在处理大量JSON数据时,有时我们需要将这些数据转换为TXT文件,以...

    python json文件转txt文件,批处理json文件转换成一个txt文件

    def json_to_txt(json_file_path, txt_file_path): with open(json_file_path, 'r', encoding='utf-8') as f: data = json.load(f) # 如果你需要提取JSON文件的特定部分,这里可以进行处理 # 比如,假设我们只...

    A JSON parser in C++

    JSON++ can also convert JSON documents into lossless XML documents. Contributors http://github.com/hjiang http://github.com/elanthis http://github.com/r-lyeh If you've made substantial ...

    使用python将plist文件转换为json格式

    其中,plist(Property List)是Apple生态系统中的一种数据存储格式,常用于iOS和macOS应用程序,而JSON(JavaScript Object Notation)则是一种轻量级的数据交换格式,被广泛应用于多种编程语言,包括Python。...

    android xml转jsonxml转json(两种,一种jar包一种工程lib)

    而JSON(JavaScript Object Notation)则是一种轻量级的数据交换格式,易于人阅读和编写,同时也易于机器解析和生成。本篇将详细讲解如何在Android中实现XML到JSON的转换,包括使用jar包和集成工程库两种方法。 ...

    excel-csv-file-convert-to-json

    文件名“excel-csv-file-convert-to-json-main”可能表示这是一个主程序文件,用于实现上述转换逻辑。这个程序可能包含了读取CSV文件、解析内容、转换为JSON并保存或输出的完整流程。 总之,使用Java进行Excel CSV...

    mdb-json:用于将 Ms Access Mdbs 转换为 json 的简单 Scala 库

    mdbToJson.convert(mdbFile, jsonOutput) ``` 这段代码展示了如何使用mdb-json库将一个Access数据库文件转换为JSON,并将结果保存到指定的JSON文件中。 **7. 扩展性和应用场景** 由于mdb-json库是用Scala编写的,它...

    gomodrun:被遗忘的go工具,用于执行和缓存go.mod文件中包含的二进制文件

    # Convert a JSON object to a Go struct, properly passing in stdin. echo example.json | gomodrun gojson > example.go # Specifiy alternative root directory containing a go.mod and tools file. ...

    jsoncpp.rar

    JSONCPP是一个开源库,用于在C++中处理JSON(JavaScript Object Notation)数据格式。它提供了解析、生成和操作JSON的能力。在这个“jsoncpp.rar”压缩包中,我们可能找到了关于如何解决使用jsoncpp时遇到的中文写入...

    C#文件操作类分享

    在给定的代码片段中,我们看到一个名为`ConvertJson`的类,它专注于将数据结构转换为JSON格式。JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,常用于在客户端和服务器之间传递数据。 `Convert...

    c#编写简单的学生管理系统

    string jsonData = File.ReadAllText("students.json"); students = JsonConvert.DeserializeObject<List<Student>>(jsonData); } } ``` 为了实现用户界面,我们可以利用Windows Forms或WPF库创建图形用户界面...

    pyon:使用JSON的Python方式-具有本机对象:hammer_and_wrench:和路径支持:file_folder:

    Pyon (Pythonic JSON)是一个Python库,可让您轻松地将本机对象转换为JSON对象。 它还支持类似文件系统的path-structure ,它使您可以按照自己喜欢的方式轻松构造JSON对象。 此外,它使用递归以便还将每个连接的...

    Face++ 1:1 人脸比对 C#实现例子

    var img1Base64 = Convert.ToBase64String(img1Bytes); var img2Base64 = Convert.ToBase64String(img2Bytes); var result = await CompareFaces("https://api-us.faceplusplus.com/facepp/v3/compare", apiKey, ...

    yield:可扩展的精益事件处理

    屈服 可扩展的精益事件处理。...# Read lines as JSON object or convert them if the former fails. toJSON # Apply a regular expression to split up the log event's message. grok message ^(?[^

    Lerner -- Python Workout. 50 Essential Exercises -- 2020.pdf

    - **Objective:** Convert an integer to its hexadecimal representation. - **Key Concepts:** - Using the `hex()` built-in function. - Understanding hexadecimal number system. 5. **Pig Latin** - **...

    asp.net上传图片预览

    string base64String = Convert.ToBase64String(imageBytes); return Json(new { previewUrl = $"data:image/{file.ContentType};base64,{base64String}" }); } return Json(null); } ``` 前端JavaScript代码...

    Highcharts本地VS2008导出

    protected void Page_Load(object sender, EventArgs e) { string chartOptions = Request["options"]; // 获取图表选项 string format = Request["type"]; // 获取导出格式,如'png', 'jpg', 'pdf', 'svg' ...

    运动医学应用

    - import pandas as pd- read excel file with .read_excel- rund file with 'python3 data.py'- used excel2json to convert excel data to json object组件VS。 转接器- Adapaters are like Containers in React ...

    VB将二进制图片文件转换为Base64字符串文本

    在VB中,我们可以使用System.Text.Encoding类的GetBytes方法将字符串转换为字节数组,然后使用Convert.ToBase64String方法将字节数组转换为Base64字符串。相反,如果你想从Base64字符串恢复原始的二进制数据,可以...

    HTML5拍照上传

    dynamic data = serializer.Deserialize(Request.InputStream.ReadToEnd(), typeof(object)); string imageData = data.image.ToString(); byte[] imageBytes = Convert.FromBase64String(imageData.Substring(...

Global site tag (gtag.js) - Google Analytics