关于如何将JSONArray转换为ListView?和如何将jsonarray转换为string的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于011-JSON、JSONObject、JS
关于如何将JSONArray转换为ListView?和如何将jsonarray转换为string的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于011-JSON、JSONObject、JSONArray使用、JSON数组形式字符串转换为List等相关知识的信息别忘了在本站进行查找喔。
本文目录一览:- 如何将JSONArray转换为ListView?(如何将jsonarray转换为string)
- 011-JSON、JSONObject、JSONArray使用、JSON数组形式字符串转换为List
- Android ArrayList ListView / Textview-您必须为textview提供资源ID
- android – 如何将jbyteArray转换为Mat?
- android – 将JSON解析为ListView友好输出
如何将JSONArray转换为ListView?(如何将jsonarray转换为string)
我有一个下面的代码-
- 通过HttpClient连接到Web服务到PHP文件
- 从SQL查询返回结果
- 返回格式是一个jArray(一个JSONArray)
for(int i=0; i < jArray.length() ; i++) {
json_data = jArray.getJSONObject(i);
int id=json_data.getInt("id");
String name=json_data.getString("name");
Log.d(name,"Output");
}
当我查看LogCat时,我看到了查询的所有“名称”,每条记录都被打印出来。我只需要将这些结果插入ListView中即可。我怎样才能做到这一点?
PS-我没有ArrayAdapter的单独类。这可能是原因吗?
011-JSON、JSONObject、JSONArray使用、JSON数组形式字符串转换为List
一、JSON数据格式
1.1、常用JSON数据格式
1、对象方式:JSONObject的数据是用 { } 来表示的,
例如: { "id" : "123", "courseID" : "huangt-test", "title" : "提交作业", "content" : null }
2、数组方式:JSONArray,顾名思义是由JSONObject构成的数组,用 [ { } , { } , ...... , { } ] 来表示
例如:[ { "id" : "123", "courseID" : "huangt-test", "title" : "提交作业" } , { "content" : null, "beginTime" : 1398873600000 "endTime" } ] ;
表示了包含2个JSONObject的JSONArray。
1.2、JSON、JSONObject、JSONArray使用
对象/数组 | toJSONString | parse | parseObject | parseArray |
JSON | 对象/数组 | 数据是对象时,返回:JSONObject的Object 数据是数组时,返回:JSONArray的Object |
对象 | 数组 |
JSONObject | 对象/数组 | 数据是对象时,返回:JSONObject的Object 数据是数组时,返回:JSONArray的Object |
对象 | 数组 |
JSONArray | 对象/数组 | 数据是对象时,返回:JSONObject的Object 数据是数组时,返回:JSONArray的Object |
对象 | 数组 |
parseObject:泛型实现 返回 具体bean;非泛型实现 返回 JSONObject
parseArray:泛型实现 返回 具体List<bean>;非泛型实现 返回 JSONArray<JSONObject>
parse:相当于parseObject和parseArray的非泛型实现
工具类Person


public class Person {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Person(String name, Integer age) {
this.name = name;
this.age = age;
}
public Person() {
}
@Override
public String toString() {
return "Person{" +
"name=''" + name + ''\'''' +
", age=" + age +
''}'';
}
}
1.2.1、数据是 对象 方式测试【parseObject】


/**
* 数据是 对象 测试
*/
public class ObjectJsonTest {
@Test
public void testtoJSONString(){
Person person = new Person("aa", 1);
String s1 = JSON.toJSONString(person);
System.out.println(s1);
String s2 = JSONObject.toJSONString(person);
System.out.println(s2);
String s3 = JSONArray.toJSONString(person);
System.out.println(s3);
}
@Test
public void testparseObject(){
Person person = new Person("aa", 1);
String s1 = JSON.toJSONString(person);
System.out.println(s1);
Person p1 = JSON.parseObject(s1, Person.class);
JSONObject jsonObject = JSON.parseObject(s1);
System.out.println(p1);
Person p2 = JSONObject.parseObject(s1, Person.class);
System.out.println(p2);
Person p3 = JSONArray.parseObject(s1, Person.class);
System.out.println(p3);
}
/**
* 失败
*/
@Test
public void testparseArray(){
Person person = new Person("aa", 1);
String s1 = JSON.toJSONString(person);
System.out.println(s1);
//失败:转数组 必须是 list
List<Person> p1 = JSON.parseArray(s1, Person.class);
System.out.println(p1);
//失败:转数组 必须是 list
List<Person> p2 = JSONObject.parseArray(s1, Person.class);
System.out.println(p2);
//失败:转数组 必须是 list
List<Person> p3 = JSONArray.parseArray(s1, Person.class);
System.out.println(p3);
}
}
1.2.2、数据是 数组 方式测试【parseArray】


/**
* 数据是 数组 测试
*/
public class ArrayJsonTest {
@Test
public void testtoJSONString(){
Person person = new Person("aa", 1);
List<Person> list =new ArrayList<>();
list.add(person);
String s1 = JSON.toJSONString(list);
System.out.println(s1);
String s2 = JSONObject.toJSONString(list);
System.out.println(s2);
String s3 = JSONArray.toJSONString(list);
System.out.println(s3);
}
//失败
@Test
public void testparseObject(){
Person person = new Person("aa", 1);
List<Person> list =new ArrayList<>();
list.add(person);
String s1 = JSON.toJSONString(list);
System.out.println(s1);
//失败:转对象 必须是 {}
Person p1 = JSON.parseObject(s1, Person.class);
System.out.println(p1);
//失败:转对象 必须是 {}
Person p2 = JSONObject.parseObject(s1, Person.class);
System.out.println(p2);
//失败:转对象 必须是 {}
Person p3 = JSONArray.parseObject(s1, Person.class);
System.out.println(p3);
}
@Test
public void testparseArray(){
Person person = new Person("aa", 1);
List<Person> list =new ArrayList<>();
list.add(person);
String s1 = JSON.toJSONString(list);
System.out.println(s1);
List<Person> p1 = JSON.parseArray(s1, Person.class);
System.out.println(p1);
List<Person> p2 = JSONObject.parseArray(s1, Person.class);
System.out.println(p2);
List<Person> p3 = JSONArray.parseArray(s1, Person.class);
System.out.println(p3);
}
}
1.2.3、非泛型示例【主要是parseObject、parseArray以及parse】


/**
* 非泛型示例
*/
public class JSONObjectJsonTest {
//数据是 对象
@Test
public void testparseObject1(){
Person person = new Person("aa", 1);
String s1 = JSON.toJSONString(person);
System.out.println(s1);
JSONObject p1 = JSON.parseObject(s1);
System.out.println(p1);
System.out.println(p1.getString("name"));
JSONObject p2 = JSONObject.parseObject(s1);
System.out.println(p2);
JSONObject p3 = JSONArray.parseObject(s1);
System.out.println(p3);
}
//数据是 对象
@Test
public void testparseObject2(){
Person person = new Person("aa", 1);
String s1 = JSON.toJSONString(person);
System.out.println(s1);
Object p1 = JSON.parse(s1);
System.out.println(p1.getClass());
System.out.println(p1);
Object p2 = JSONObject.parse(s1);
System.out.println(p2.getClass());
System.out.println(p2);
Object p3 = JSONArray.parse(s1);
System.out.println(p3.getClass());
System.out.println(p3);
}
//数据是 数组
@Test
public void testparseArray1(){
Person person = new Person("aa", 1);
List<Person> list =new ArrayList<>();
list.add(person);
String s1 = JSON.toJSONString(list);
System.out.println(s1);
JSONArray p1 = JSON.parseArray(s1);
System.out.println(p1);
JSONArray p2 = JSONObject.parseArray(s1);
System.out.println(p2);
JSONArray p3 = JSONArray.parseArray(s1);
System.out.println(p3);
}
//数据是 数组
@Test
public void testparseArray2(){
Person person = new Person("aa", 1);
List<Person> list =new ArrayList<>();
list.add(person);
String s1 = JSON.toJSONString(list);
System.out.println(s1);
JSONArray p1 = (JSONArray)JSON.parse(s1);
System.out.println(p1.getClass());
System.out.println(p1);
Object p2 = JSONObject.parse(s1);
System.out.println(p2.getClass());
System.out.println(p2);
Object p3 = JSONArray.parse(s1);
System.out.println(p3.getClass());
System.out.println(p3);
}
}
二、JSON数组形式字符串转换为List<Map<String,String>>的8种方法
public class JsonToLIstMapTest {
String strArr = "[{\"0\":\"zhangsan\",\"1\":\"lisi\",\"2\":\"wangwu\",\"3\":\"maliu\"}," +
"{\"00\":\"zhangsan\",\"11\":\"lisi\",\"22\":\"wangwu\",\"33\":\"maliu\"}]";
@Test
public void test(){
//第一种方式
List<Map<String,String>> listObjectFir = (List<Map<String,String>>) JSONArray.parse(strArr);
System.out.println("1、利用JSONArray中的parse方法来解析json数组字符串");//com.alibaba.fastjson.JSONObject
System.out.println("类型:"+listObjectFir.get(0).getClass());
for(Map<String,String> mapList : listObjectFir){
for (Map.Entry entry : mapList.entrySet()){
System.out.println( entry.getKey() + " " +entry.getValue());
}
}
//第二种方式
List<Map<String,String>> listObjectSec = JSONArray.parseObject(strArr,List.class);
System.out.println("2、利用JSONArray中的parseObject方法并指定返回类型来解析json数组字符串");//com.alibaba.fastjson.JSONObject
System.out.println("类型:"+listObjectSec.get(0).getClass());
for(Map<String,String> mapList : listObjectSec){
for (Map.Entry entry : mapList.entrySet()){
System.out.println( entry.getKey() + " " +entry.getValue());
}
}
//第三种方式
JSONArray listObjectThir = JSONArray.parseArray(strArr);
System.out.println("3、利用JSONArray中的parseArray方法来解析json数组字符串");
System.out.println("类型:"+listObjectThir.get(0).getClass());//com.alibaba.fastjson.JSONObject
for(Object mapList : listObjectThir){
for (Object entry : ((Map)mapList).entrySet()){
System.out.println(((Map.Entry)entry).getKey() + " " +((Map.Entry)entry).getValue());
}
}
//第四种方式
List listObjectFour = JSONArray.parseArray(strArr,Map.class);
System.out.println("4、利用JSONArray中的parseArray方法并指定返回类型来解析json数组字符串");
System.out.println("类型:"+listObjectFour.get(0).getClass());//java.util.HashMap
for(Object mapList : listObjectFour){
for (Object entry : ((Map)mapList).entrySet()){
System.out.println(((Map.Entry)entry).getKey() + " " +((Map.Entry)entry).getValue());
}
}
//第五种方式
JSONArray listObjectFifth = JSONObject.parseArray(strArr);
System.out.println("5、利用JSONObject中的parseArray方法来解析json数组字符串");
System.out.println("类型:"+listObjectFifth.get(0).getClass());//com.alibaba.fastjson.JSONObject
for(Object mapList : listObjectFifth){
for (Object entry : ((Map)mapList).entrySet()){
System.out.println(((Map.Entry)entry).getKey() + " " +((Map.Entry)entry).getValue());
}
}
//第六种方式
List listObjectSix = JSONObject.parseArray(strArr,Map.class);
System.out.println("6、利用JSONObject中的parseArray方法并指定返回类型来解析json数组字符串");
System.out.println("类型:"+listObjectSix.get(0).getClass());//java.util.HashMap
for(Object mapList : listObjectSix){
for (Object entry : ((Map)mapList).entrySet()){
System.out.println(((Map.Entry)entry).getKey() + " " +((Map.Entry)entry).getValue());
}
}
//第七种方式
JSONArray listObjectSeven = JSON.parseArray(strArr);
System.out.println("7、利用JSON中的parseArray方法来解析json数组字符串");
System.out.println("类型:"+listObjectSeven.get(0).getClass());//com.alibaba.fastjson.JSONObject
for(Object mapList : listObjectSeven){
for (Object entry : ((Map)mapList).entrySet()){
System.out.println(((Map.Entry)entry).getKey() + " " +((Map.Entry)entry).getValue());
}
}
//第八种方式
List listObjectEigh = JSONObject.parseArray(strArr,Map.class);
System.out.println("8、利用JSON中的parseArray方法并指定返回类型来解析json数组字符串");
System.out.println("类型:"+listObjectEigh.get(0).getClass());//java.util.HashMap
for(Object mapList : listObjectEigh){
for (Object entry : ((Map)mapList).entrySet()){
System.out.println(((Map.Entry)entry).getKey() + " " +((Map.Entry)entry).getValue());
}
}
}
}
Android ArrayList ListView / Textview-您必须为textview提供资源ID
我曾经在某一时刻进行过这项工作,所以我认为我所引用的东西有误。_regionListView似乎返回确定。我认为ArrayList中的数组是问题,因为它包含额外的空索引,但我认为我没有正确连接到ListView和TextView。
任何帮助,将不胜感激。
Main.java
String _region = inRegion;
ParserRegion _parserRegion = new ParserRegion();
InputStream _inputStream = getResources().openRawResource(R.raw.regions);
// Parse the Input Stream
_parserRegion.Parse(_inputStream,_region);
// Get Regions
List<PropertiesRegion> _regionList = _parserRegion.GetList();
// Create the ArrayAdapter
ArrayAdapterRegion _arrayAdapter = new ArrayAdapterRegion(getApplicationContext(),R.layout.search_list,_regionList);
// Get reference to ListView holder
ListView _regionListView = (ListView) this.findViewById(R.id.regionListView);
// Set the ListView adapter
_regionListView.setAdapter(_arrayAdapter);
Search.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:adjustViewBounds="true"
android:background="@drawable/search_background"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:scaleType="centerCrop" >
<TextView
android:id="@+id/selectRegionTextView"
android:cacheColorHint="#00000000"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:layout_marginTop="150dip"
android:gravity="top"
android:textColor="#000000"
android:textSize="20dip"
android:textandroid:typeface="sans" />
<ListView
android:id="@+android:id/regionListView"
android:cacheColorHint="#00000000"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="5dip"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:padding="10dp" />
<Button
android:id="@+id/mainMenuButton"
android:background="@drawable/button_black"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginBottom="70dip"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:typeface="sans"
android:textSize="18dip"
android:textandroid:textColor="@android:color/white"/>
</LinearLayout>
Search_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/regionTextView"
android:cacheColorHint="#00000000"
android:gravity="center"
android:layout_gravity="left"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:paddingLeft="10dip"
android:textColor="#000000"
android:typeface="sans"
android:textSize="20dip"
android:text/>
</LinearLayout>
android – 如何将jbyteArray转换为Mat?
从ByteArrayOutpuStream.toByteArray()返回的byteArray原来是一个.bmp图像.这意味着byteArray也有imageHeader.
在Native端我有jbyteArray存储byteArray.
现在,如何将此jbyteArray转换为Mat进行进一步处理?
我想我必须先用cv :: imdecode(?,?,?)解码它?
谢谢
解决方法
请参阅以下链接.
https://github.com/ethanrublee/catkin-opencv/blob/master/samples/android/tutorial-3-native/jni/jni_part.cpp
android-opencv项目有这些样本.
android – 将JSON解析为ListView友好输出
{"popular": {"authors_last_month": [ { "url":"http://activeden.net/user/OXYLUS","item":"OXYLUS","sales":"1148","image":"http://s3.envato.com/files/15599.jpg" },{ "url":"http://activeden.net/user/digitalscience","item":"digitalscience","sales":"681","image":"http://s3.envato.com/files/232005.jpg" } { ... } ],"items_last_week": [ { "cost":"4.00","thumbnail":"http://s3.envato.com/files/227943.jpg","url":"http://activeden.net/item/christmas-decoration-balls/75682","sales":"43","item":"Christmas decoration Balls","rating":"3","id":"75682" },{ "cost":"30.00","thumbnail":"http://s3.envato.com/files/226221.jpg","url":"http://activeden.net/item/xml-flip-book-as3/63869","sales":"27","item":"XML Flip Book / AS3","rating":"5","id":"63869" },{ ... }],"items_last_three_months": [ { "cost":"5.00","thumbnail":"http://s3.envato.com/files/195638.jpg","url":"http://activeden.net/item/image-logo-shiner-effect/55085","sales":"641","item":"image logo shiner effect","id":"55085" },{ "cost":"15.00","thumbnail":"http://s3.envato.com/files/180749.png","url":"http://activeden.net/item/banner-rotator-with-auto-delay-time/22243","sales":"533","item":"BANNER rotator with Auto Delay Time","id":"22243"},{ ... }] } }
它也可以被访问here,虽然因为它是一个很长的字符串,我已经修改了上面的内容以显示所需的内容.
基本上,我希望能够访问“items_last_week”中的项目并创建它们的列表 – 最初我的计划是在左侧显示“缩略图”,旁边有“项目”,但是从今天的SDK看起来太难或不可能实现这一点,所以我很高兴只从列表中的’items_last_week’获取’item’数据.
来自PHP我很难使用任何可用于Java的JSON库,因为它看起来不仅仅是一行代码,我需要反序列化(我认为这是正确的词)JSON,以及它们似乎都需要某种形式的附加类,除了JSONArray / JSONObject脚本我不喜欢items_last_week嵌套的事实(再次,我认为这是JSON术语)并且需要很长时间才能运行Android模拟器.
因此,实际上,我需要一种(最好是简单的)方法将items_last_week数据传递给ListView.我知道我需要一个自定义适配器,我可能会理解,但我无法理解,无论我花了多少时间试图找出它,如何访问JSON字符串的某些部分..
解决方法
originally my plan was to have the
‘thumbnail’ on the left with the
‘item’ next to it,but from playing
around with the SDK today it appears
too difficult or impossible to achieve
this
这远非不可能,但除非你为你使用something that already wraps up that pattern(并且希望合理地说“正确”),否则做出正确的做法将是乏味的.在Web上,性能/带宽问题是用户的问题 – 在移动设备中,它们是您的问题.
as it appears to be much more than a
line of code which I will need to
deserialize (I think that’s the right
word) the JSON
新的JSONObject(数据)是一行代码.现在,从前面提到的URL获取JSON(我认为你正在做的)将是几行代码.解析JSON或从互联网上获取它都不是Android独有的 – 所有这些在桌面Java应用程序或Java servlet或其他任何东西上看起来都是一样的.
apart from the JSONArray/JSONObject
script I have which doesn’t like the
fact that items_last_week is nested
我没有遇到像文件展示这样的结构解析JSON的问题.此外,这几乎不是Android独有的 – JSON解析器在许多其他基于Java的项目中使用.
and takes an awful long time to run on
the Android emulator
仿真器的速度与开发机器的速度有关.对我来说,模拟器通常比实际的手机硬件慢…而我的桌面是四核的.请记住,仿真器假装是在PC上运行的ARM芯片组,即时将ARM操作码转换为x86操作码,因此它不会很快,也不会很好地利用多个内核.
So,in effect,I need a (preferably
simple) way to pass the
items_last_week data to a ListView.
Android中没有任何内置功能可以使用任意数据采用任意JSON结构,并将其直接转换为ListView.这不是JSON独有的 – XML会出现类似的现象.
你的选择是:
>创建一个包装已解析JSON的自定义listadapter.
>将解析后的JSON转换为MatrixCursor(想想2D数据数组)并使用SimpleCursorAdapter.
>将解析后的JSON转换为ArrayList< String>并使用ArrayAdapter.
从短期来看,选项#3可能是最简单的.
I understand I will need a custom
adapter which I can probably get my
head around but I cannot understand,
no matter how much of the day I’ve
just spent trying to figure it out,
how to access certain parts of a JSON
string..
这个问题在援助方面太过模糊.您可以考虑打开一个单独的问题,标记为Java和JSON,您可以在其中详细了解json.org解析器遇到问题的位置.
关于如何将JSONArray转换为ListView?和如何将jsonarray转换为string的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于011-JSON、JSONObject、JSONArray使用、JSON数组形式字符串转换为List的相关知识,请在本站寻找。
本文标签: