-1

我在解析 Android 中的 JSON 包时遇到了一些困难。

我目前已经完成了所有设置,因此 JSON 是一个对象数组,然后每个对象都有一个属性数组。例如,假设我的包中有一个名为 Colors 的对象。然后每个 Color 条目将位于 Colors 对象中。每个颜色条目也将包含 R、G、B 值的条目。

这种类型,我可以应付得很好。但是,我现在遇到了一个实例,其中其中一个条目(R、G、B 值所在的位置)中有一个数组。我不知道如何去访问和处理它。

我将使用 JSON 包的示例进行更新,因为我担心我不是很清楚。

编辑:这是 JSON。假设我想访问 ColorOverlays 中的 R 值。

{"Package":[ {"Things":[{"ProgramId":73,"TypeId":68,"CategoryId":null,"CategoryName":null,"ThingId":121,"ThingName":"Mahalo" ,"ThingDescription":"开始您的节日并拍摄一些照片!","ThingPrice":0.00,"SellerProductId":null,"Number2":1342655700,"Number1":1342655700,"IsAvailable":true,"ImageOverlays" :[{"ThingId":121,"ThingOverlayId":295,"ImageOverlayBase64":null,"ImageOverlayFileTypeExtension":null,"Width":1024,"Height":1024,"A":1.00000,"BlendModeId":1 ,"OrderNum":2,"IsUseSource":false}],"ColorOverlays":[{"ThingId":121,"ThingOverlayId":294,"R":157.00000,"G":71.00000,"B":187.00000,"A":0.52873,"BlendModeId":6,"OrderNum":1}],"ThingsampleImageBase64":null,"ThingsampleImageFileTypeExtension":"", "ThingsampleImageWidth":546,"ThingsampleImageHeight":546,"Captures":[{"ThingCaptureId":87,"ThingId":121,"CaptureFrameOverlayId":null,"IsRemoved":false,"AddDate":1342637814,"LastUpdated ":1342637814,"饱和度":0.0,"对比度":0.0,"亮度":0.0,"低":null,"中":null,"高":null,"IsBlackWhite":null,"IsInvert": null,"IsSepia":null}],"IsRemoved":false},{"ProgramId":73,"TypeId":68,"CategoryId":null,"CategoryName":null,"ThingId":122,"ThingName":"Lots of Love","ThingDescription":"使用这个!","ThingPrice":0.00,"SellerProductId":null,"Number2":1342667100 ,"Number1":1342667100,"IsAvailable":true,"ImageOverlays":[{"ThingId":122,"ThingOverlayId":298,"ImageOverlayBase64":null,"ImageOverlayFileTypeExtension":null,"Width":1024,"高度":1024,"A":1.00000,"BlendModeId":4,"OrderNum":3,"IsUseSource":false}],"ColorOverlays":[{"ThingId":122,"ThingOverlayId":296," R":213.00000,"G":86.00000,"B":143.00000,"A":0.77777,"BlendModeId":4,"OrderNum":1},{"ThingId":122,"ThingOverlayId":297,"R":127.00000,"G":127.00000,"B":127.00000,"A":0.50000,"BlendModeId":1,"OrderNum":2}]," ThingsampleImageBase64":null,"ThingsampleImageFileTypeExtension":"","ThingsampleImageWidth":546,"ThingsampleImageHeight":546,"Captures":[{"ThingCaptureId":88,"ThingId":122,"CaptureFrameOverlayId":null,"IsRemoved ":false,"AddDate":1342649164,"LastUpdated":1342649164,"Saturation":0.0,"Contrast":0.0,"Brightness":0.0,"Low":null,"Mid":null,"High": null,"IsBlackWhite":null,"IsInvert":null,"IsSepia":null}],"IsRemoved":false}]} ]}ThingOverlayId":297,"R":127.00000,"G":127.00000,"B":127.00000,"A":0.50000,"BlendModeId":1,"OrderNum":2}],"ThingsampleImageBase64":null," ThingsampleImageFileTypeExtension":"","ThingsampleImageWidth":546,"ThingsampleImageHeight":546,"Captures":[{"ThingCaptureId":88,"ThingId":122,"CaptureFrameOverlayId":null,"IsRemoved":false,"AddDate ":1342649164,"LastUpdated":1342649164,"Saturation":0.0,"Contrast":0.0,"Brightness":0.0,"Low":null,"Mid":null,"High":null,"IsBlackWhite": null,"IsInvert":null,"IsSepia":null}],"IsRemoved":false}]} ]}ThingOverlayId":297,"R":127.00000,"G":127.00000,"B":127.00000,"A":0.50000,"BlendModeId":1,"OrderNum":2}],"ThingsampleImageBase64":null," ThingsampleImageFileTypeExtension":"","ThingsampleImageWidth":546,"ThingsampleImageHeight":546,"Captures":[{"ThingCaptureId":88,"ThingId":122,"CaptureFrameOverlayId":null,"IsRemoved":false,"AddDate ":1342649164,"LastUpdated":1342649164,"Saturation":0.0,"Contrast":0.0,"Brightness":0.0,"Low":null,"Mid":null,"High":null,"IsBlackWhite": null,"IsInvert":null,"IsSepia":null}],"IsRemoved":false}]} ]}00000,"G":127.00000,"B":127.00000,"A":0.50000,"BlendModeId":1,"OrderNum":2}],"ThingsampleImageBase64":null,"ThingsampleImageFileTypeExtension":"","ThingsampleImageWidth" :546,"ThingsampleImageHeight":546,"Captures":[{"ThingCaptureId":88,"ThingId":122,"CaptureFrameOverlayId":null,"IsRemoved":false,"AddDate":1342649164,"LastUpdated":1342649164 ,"饱和度":0.0,"对比度":0.0,"亮度":0.0,"低":null,"中":null,"高":null,"IsBlackWhite":null,"IsInvert":null," IsSepia":null}],"IsRemoved":false}]} ]}00000,"G":127.00000,"B":127.00000,"A":0.50000,"BlendModeId":1,"OrderNum":2}],"ThingsampleImageBase64":null,"ThingsampleImageFileTypeExtension":"","ThingsampleImageWidth" :546,"ThingsampleImageHeight":546,"Captures":[{"ThingCaptureId":88,"ThingId":122,"CaptureFrameOverlayId":null,"IsRemoved":false,"AddDate":1342649164,"LastUpdated":1342649164 ,"饱和度":0.0,"对比度":0.0,"亮度":0.0,"低":null,"中":null,"高":null,"IsBlackWhite":null,"IsInvert":null," IsSepia":null}],"IsRemoved":false}]} ]}BlendModeId":1,"OrderNum":2}],"ThingsampleImageBase64":null,"ThingsampleImageFileTypeExtension":"","ThingsampleImageWidth":546,"ThingsampleImageHeight":546,"Captures":[{"ThingCaptureId":88, "ThingId":122,"CaptureFrameOverlayId":null,"IsRemoved":false,"AddDate":1342649164,"LastUpdated":1342649164,"Saturation":0.0,"Contrast":0.0,"Brightness":0.0,"Low ":null,"Mid":null,"High":null,"IsBlackWhite":null,"IsInvert":null,"IsSepia":null}],"IsRemoved":false}]} ]}BlendModeId":1,"OrderNum":2}],"ThingsampleImageBase64":null,"ThingsampleImageFileTypeExtension":"","ThingsampleImageWidth":546,"ThingsampleImageHeight":546,"Captures":[{"ThingCaptureId":88, "ThingId":122,"CaptureFrameOverlayId":null,"IsRemoved":false,"AddDate":1342649164,"LastUpdated":1342649164,"Saturation":0.0,"Contrast":0.0,"Brightness":0.0,"Low ":null,"Mid":null,"High":null,"IsBlackWhite":null,"IsInvert":null,"IsSepia":null}],"IsRemoved":false}]} ]}546,"ThingsampleImageHeight":546,"Captures":[{"ThingCaptureId":88,"ThingId":122,"CaptureFrameOverlayId":null,"IsRemoved":false,"AddDate":1342649164,"LastUpdated":1342649164, "饱和度":0.0,"对比度":0.0,"亮度":0.0,"低":null,"中":null,"高":null,"IsBlackWhite":null,"IsInvert":null,"IsSepia ":null}],"IsRemoved":false}]} ]}546,"ThingsampleImageHeight":546,"Captures":[{"ThingCaptureId":88,"ThingId":122,"CaptureFrameOverlayId":null,"IsRemoved":false,"AddDate":1342649164,"LastUpdated":1342649164, "饱和度":0.0,"对比度":0.0,"亮度":0.0,"低":null,"中":null,"高":null,"IsBlackWhite":null,"IsInvert":null,"IsSepia ":null}],"IsRemoved":false}]} ]}null,"Mid":null,"High":null,"IsBlackWhite":null,"IsInvert":null,"IsSepia":null}],"IsRemoved":false}]} ]}null,"Mid":null,"High":null,"IsBlackWhite":null,"IsInvert":null,"IsSepia":null}],"IsRemoved":false}]} ]}

4

2 回答 2

1

您的代码示例对尝试提供帮助最有帮助。取决于您的 RGB 值数组的构建和传递方式。如果它是一个简单的逗号分隔字符串,那么你可以这样做:

try {

        JSONArray jArray = new JSONArray(jString);

        for (int i=0; i<jArray.length(); i++) {

            JSONObject jo = jArray.getJSONObject(i);
                    String RGBVal = jo.getString("rgb_list");
                    String[] rgbArray = RGBVal.split(",");
                    ....

同样,取决于条目在 JSON 字符串中的传递方式。

没关系...

于 2012-07-20T14:56:04.490 回答
1

如果这不是您在第一次使用后会丢弃的东西,那么您可能会考虑通过在您的域之后建模 Java 对象来提高它的水平。

使用任何在线 JSON 可视化工具以 3D 形式查看您的 JSON。

然后,按照以下教程进行操作。

http://java.sg/parsing-a-json-string-into-an-object-with-gson-easily/

于 2012-07-20T15:05:47.673 回答