使用org.json在Java中解析JSON [英] Parsing JSON in Java Using org.json

查看:143
本文介绍了使用org.json在Java中解析JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大型文件,其中包含许多类似于以下内容的JSON对象.我需要使用 org.json将所有内容解析为数组形式的"bought_together"项目库.我无法访问嵌套在相关"中的任何内容.

I have a large file with many JSON objects similiar to the following. I need to parse everything to get the "bought_together" items as an array using the org.json library. I'm having trouble accessing anything nested in "related".

将"bought_together"作为列表检索所需的代码是什么?

What is the required code to retrieve "bought_together" as a list?

{
  "asin": "11158732",
  "title": "Girls Ballet Tutu Zebra Hot Pink",
  "price": 3.17,
  "imUrl": "http://ecx.images-amazon.com/images/I/51fAmVkTbyL._SY300_.jpg",
  "related":
  {
    "also_bought": ["L00JHONN1S", "B002BZX8Z6"],
    "also_viewed": ["F002BZX8Z6", "B00JHONN1S", "B008F0SU0Y", "B00D23MC6W", "B00AFDOPDA"],
    "bought_together": ["D202BZX8Z6"]
  },
  "salesRank": {"Toys & Games": 211836},
  "brand": "Coxlures",
  "categories": [["Sports & Outdoors", "Other Sports", "Dance"]]
}

这是我的尝试(请注意,这是在MapReduce程序中,因此某些行可能看起来没有上下文.):

Here is my attempt (Please note, this is within a MapReduce program so some lines may seem out of context.):

JSONObject object = new JSONObject(sampleText); //sampleText is json that has been split by line
JSONArray boughtTogether = new JSONArray(object.getJSONArray("bought_together"));

推荐答案

使用以下代码,希望对您有所帮助.

using the following code, I hope it's help you.

//this will be your json object that contains and convert your string to jsonobject
//if you have json object already skip this.
JSONObject yourJSON = new JSONObject(targetString);

//getting the "related" jsonObject
JSONObject related = yourJSON.getJSONObject("related");

//getting the "bought_together" as an jsonArray and do what you want with it.
//you can act with jsonarray like an array
JSONArray bought_together = related.getJSONArray("bought_together");


//now if you run blow code

System.out.print(bought_together.getString(0));

//output is : D202BZX8Z6

---------根据更新问题进行更新------ 您应该这样更改代码:

-------update according to update the question------ you should change your code like this:

JSONObject object = new JSONObject(sampleText); //sampleText is json that has been split by line

JSONObject related = object.getJSONObject("related");

JSONArray boughtTogether = related.getJSONArray("bought_together");

-------更新-------

-------update-------

我认为您需要注意这一点(这不是技术上的差异)

i think you need to this point (it's not technicality all of they difference)

  • 每件事都在{}中,它们将是JSONObject及其关系 是关键和价值,如:

  • every thing are in {} , they will be JSONObject and the relation is key and value like :

{名称":"ali"}

{"name":"ali"}

这是一个jsonobject,键"name"的值是ali,我们称之为 像:

this is a jsonobject and the value of key "name" is ali and we call it like:

myJsonObject.getString("name");

myJsonObject.getString("name");

每件事都在[]中,它们将是JSONArray并且关系是 索引和值,如:

every thing are in [] ,they will be JSONArray and the relation is index and value like :

["ali"]

这是一个JsonArray,索引0的值是ali,我们称之为
像:

this is a JsonArray the value of index 0 is ali and we call it
like:

myJsonArray.getString(0);

myJsonArray.getString(0);

所以在您的情况下:

  1. 您的总对象是一个JSONObject
  2. 相关"键的值仍然是JSONObject
  3. "bought_together"键的值(位于{jsonobject}"related"键的值之内)是JSONArray

这篇关于使用org.json在Java中解析JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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