卡壳了... 想了好久都没想出来

爱新觉罗玄楠 发布于 2015/11/02 19:54
阅读 353
收藏 0
{ "S1":[ {"TB":"U1","COL":"N1","CT":"名字1"}, {"TB":"U2","CL":"N2","CT":"名字2"} ], "S2":[ {"TB":"U11","CL":"N11","CT":"名字11"}, {"TB":"U22","COL":"N22","CT":"名字22"} ], "DR":[ {"KE":"Y1","CK":"c1","TP":"R1"}, {"KE":"Y11","CK":"c11","TP":"R11"} ], } 这个json格式 使用java如何取他们的值出来组成下面这个二维数组? { {"U1","N1","名字1","U11","N11","名字11","Y1","c1","R1"}, {"U2","N2","名字2","U22","N22","名字22","Y11","c11","R11"} }
加载中
0
南湖船老大
南湖船老大

这有什么好卡壳。。。虽然你这JSON看着很费劲,但这不是很好解析么

public static void main(String[] args) {
		String json = "{ \"S1\":[ {\"TB\":\"U1\",\"COL\":\"N1\",\"CT\":\"名字1\"}, {\"TB\":\"U2\",\"CL\":\"N2\",\"CT\":\"名字2\"} ], \"S2\":[ {\"TB\":\"U11\",\"CL\":\"N11\",\"CT\":\"名字11\"}, {\"TB\":\"U22\",\"COL\":\"N22\",\"CT\":\"名字22\"} ], \"DR\":[ {\"KE\":\"Y1\",\"CK\":\"c1\",\"TP\":\"R1\"}, {\"KE\":\"Y11\",\"CK\":\"c11\",\"TP\":\"R11\"} ]}";
		Map<String, Object> map = JSON.parseObject(json);
		List<List> li=new ArrayList<List>();
		for (Entry<String, Object> v1 : map.entrySet()) {
			JSONArray m2 = (JSONArray) v1.getValue();
			int count=m2.size();
			for (Object object : m2) {
				Map<String, Map> v3 = (Map) object;
				List<String> l=new ArrayList<>();
				String value=null;
				for (Map.Entry entry : v3.entrySet()) {
					 l.add((String) entry.getValue());
				}
				int index=--count;
				System.out.println(l+"----"+index);
			}
		}	
	}



爱新觉罗玄楠
爱新觉罗玄楠
谢谢! 脑子转不过弯来。
0
爱新觉罗玄楠
爱新觉罗玄楠
[17]不是吧!格式这么乱...
0
eechen
eechen
PHP可以这样做:
<?php
header('Content-Type: text/plain;charset=utf-8');
$json = '{"S1":[{"TB":"U1","COL":"N1","CT":"名字1"},{"TB":"U2","CL":"N2","CT":"名字 2"}],"S2":[{"TB":"U11","CL":"N11","CT":"名字11"}, {"TB":"U22","COL":"N22","CT":"名字22"}],"DR": [{"KE":"Y1","CK":"c1","TP":"R1"},{"KE":"Y11","CK":"c11","TP":"R11"}]}';
$arr = json_decode($json,true);
foreach($arr as $v) {
    $i = 0;
    foreach($v as $v2) {
        foreach($v2 as $v3) {
            $new[$i][] = $v3;
        }
        $i++;
    }
}
echo json_encode($new, JSON_UNESCAPED_UNICODE);

Firefox的代码草稿纸或者Firebug的控制台都可以格式化JS代码和JSON串.
楼主要从:
{
  'S1': [
    {
      'TB': 'U1',
      'COL': 'N1',
      'CT': '名字1'
    },
    {
      'TB': 'U2',
      'CL': 'N2',
      'CT': '名字2'
    }
  ],
  'S2': [
    {
      'TB': 'U11',
      'CL': 'N11',
      'CT': '名字11'
    },
    {
      'TB': 'U22',
      'COL': 'N22',
      'CT': '名字22'
    }
  ],
  'DR': [
    {
      'KE': 'Y1',
      'CK': 'c1',
      'TP': 'R1'
    },
    {
      'KE': 'Y11',
      'CK': 'c11',
      'TP': 'R11'
    }
  ]
}

得到:
[
  ['U1',
  'N1',
  '名字1',
  'U11',
  'N11',
  '名字11',
  'Y1',
  'c1',
  'R1'],
  [
    'U2',
    'N2',
    '名字2',
    'U22',
    'N22',
    '名字22',
    'Y11',
    'c11',
    'R11'
  ]
]

爱新觉罗玄楠
爱新觉罗玄楠
谢谢回复!格式这么乱是因为我在手机上发布的提问,我知道怎么得到格式化的json。我主要是不知道怎么用java把这个格式的json取值出来组成下面的那个二维数组。
0
wuyiw
wuyiw

http://www.oracle.com/technetwork/articles/java/json-1973242.html

JAVA处理json不难呀, 有什么问题吗?

返回顶部
顶部