English 中文(简体)
我能否将一个阵列分割成多个变量?
原标题:Can I divide an array or string into multiple variables?

Here s where I ve been:
I made a PHP client call via soap to retrieve an array of data,
I successfully receieved and converted my array to JSON via json_encode,
I then echoed it back to my page.

Here s where I am:
I get back my array in this format...
{"MethodName":"ID,11|1|Item1,22|2|Item2,33|3|Item3"}

Here s where I want to be:
Using Javascript or JSON, my objective is to end up with 2 variables (Method & ID) and one variable array (ItemList)...ie

  • var Method = "MethodName";
  • var ID = "ID";
  • var ItemList = [ 11|1|Item1 , 22|2|Item2 , 33|3|Item3 ];

I have the front and back of my script working, but I m stumped on the array(string)...
How do I parse, divide, or split this result?

最佳回答

i 可能首先尝试reg。 类似情况

var match = data.match(/{"(.+)":"([^,]+),(.+)"}/);

母体将分开。 <代码>match[0]将是整件,match >为方法名称,match 为背式编号,match[3]为其余部分。 那么,你可以这样做。

var itemList = match[3].split( , );

进入第三期。

问题回答

如果在你称之为json_encode之前,你会把它作为综合阵列,那么你会更简便(这样,每个项目都不是自己的JSON物体)。

例:

$arr = array();
$itemList = array();
$itemList[1] = "11|1";
$itemList[2] = "22|2";
$itemList[3] = "33|3";

$arr[ Method ]="MethodName";
$arr[ ID ]= "ID";
$arr[ itemList ] = $itemList;

$output = json_encode($arr);

结果是<代码>产出。 组成如下:

{"Method":"MethodName","ID":"ID","itemList":{"1":"11|1","2":"22|2","3":"33|3"}}

那么,你可以轻易撤出,因为手工艺品已经是JSON的一个阵列。

请允许我说,你的阵列是初步的——具有价值{“MethName”:

for (key in initial_array) {  
  Method = key;                     // Method = "MethodName"
  break;
}
myvalue = initial_array[Method];
myarray = myvalue.split(",");      // myarray = ["ID","11|1|Item1",...]
ID = myarray[0];                   // ID = "ID"
ItemList = myarray.splice(1);      // ItemList = ["11|1|Item1","22|2|Item2",...] 

各位:

var myobject = {"MethodName":"ID,11|1|Item1,22|2|Item2,33|3|Item3"};
var itemlist = [];
var method;
var ID;

for (key in myobject) { 
    method = key;
    break;
}

itemlist = myobject[method].split( , );
ID = itemlist.shift();

这里是一份供你经营的联合材料。





相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签